Essay of Perl for Myself

Perl

1 Perl的标准文件头:

#!/usr/bin/perl -w
其中#!/usr/bin/perl代表perl的安装位置。
其中-w代表内建警告信息。

2 Perl的输入和输出:

3 Perl的正则表达式:

4 我目前写的Perl脚本:


#!/usr/bin/perl -w
print "##### Auto Test Start #####\n";

$caseFile = 'yourFileName.txt';

#Wait for case file
print "Wait for $caseFile\n";
while( ! open CASE, "<$caseFile" ){
    sleep 1;
    print ".\n";
}
close CASE;

#Convert format From Windows to Linux & open file
system "dos2unix $caseFile";
if( ! open CASE, "<$caseFile" ){
    die "Open $caseFile false: ($!)";
}

#Read content of case file
while(  ){
	print "## input is: $_";

	if( /^#MakeDir *= *'.*'/ ){
    	s/^#MakeDir *= *'(.*)'/$1/;

    	$targetPath = $_;
    	chomp($targetPath);
    	print "Have targetPath: $targetPath\n";
		$haveTgtPath = 1;
	}
    elsif( /^#Binname *= *'.*'/ ){
    	s/^#Binname= *'(.*)'/$1/;

    	$targetFile = $_;
    	chomp($targetFile);
    	print "Have targetFile: $targetFile\n";
		$haveTgtFile = 1;
    }
}

#Do test case
if( ($haveTgtFile == 1) & ( $haveTgtPath == 1) ){
	$tgt = "$targetPath$targetFile";
	print "Have tgt: $tgt\n";
    open LINES, "$tgt|" or die "cannot pipe from $targetFile: $!";
    while ( $line =  ){
        chomp($line);
        print "> $line\n";
        if( $line eq 'OK'){
            print "test ok\n";
            $ret = 1;
        }
    }
    close LINES;

    if( $ret != 1 ){
        close CASE;
        die "test false\n";
    }
}

#Convert format From Linux to Windows & close file
system "unix2dos $caseFile";
close CASE;

#Write result to file
open CASE, "<$caseFile" or die "cannot open $caseFile: $!";
open BACKUP, ">backup" or die "cannot open backup: $!";
while(  ){
	if ( /^#\(ExResult:n\)/ ) {
    	s/^#\(ExResult:n\)/#\(ExResult:passed\)/g;
    	print "$_";
	}
	print BACKUP $_;
}
close CASE;
close BACKUP;
system "mv backup $caseFile";
system "rm -rf backup";

print "##### Auto Test Success ! #####\n";



你可能感兴趣的:(perl)