$InFile=$ARGV[0] or die "Error! Usage: xxx.pl InFile\n";
$OutFile=$ARGV[1] or ($OutFile=$InFile . "_new") ;
open(readIn, "<$InFile") or die "Can't read the file: $InFile.\n";
open(writeOut, ">$OutFile") or die "Can't write to the file: $OutFile.\n";
my $comment_flag = 0 ;
while($line=<readIn>){
$line=~/^\s*begin\s*$/ and $comment_flag = 1 ;
if($comment_flag==1){
$line =~ s/^/**/;
}
printf( writeOut "$line");
$line=~/^\s*end\s*$/ and $comment_flag = 0 ;
}
这个程序会把begin和end之间的空白行也加上“**”,看起来这时楼主所不需要的。我是初学perl啊,自己仿照这位高手也写了一个程序,欢迎大家扔砖,讨论,多交流才能更快进步!
#!perl -w
$^I ="~";
for my $files (@ARGV){
die "no such file" if not -e $files;
}
my $find = 0;
while(<>){
$find = 1 if /^\s*begin\s*/;
s/^(\s*\w+\s*)/\*\*$1/ if $find;
$find = 0 if /^\s*end\s*/;
print;
}