需求:
现有一文件,名为test.sh,里面的内容如下export BUILD_DINER_PID="1016"
export TEST1="C:\Users\test"
export TOOL=""C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.com""
export SOURCE="C:\Users\project"
现要把这个文件的内容改为
set TEST1=C:\Users\test
set TOOL="C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.com"
set SOURCE=C:\Users\project
并存入test.pl文件里。
- #!/usr/bin/perl -w
-
#
-
use strict;
-
-
our $filename = 'build_env_old.sh';
-
-
sub replace_func{
-
our $filename;
-
(my $newfile = $filename ) =~ s/\.sh/.bat/;
-
print $newfile;
-
-
open(FILE, $filename) or die "Cannot open the file\n";
-
-
if (-e $newfile){
-
unlink $newfile;
-
}
-
-
open(NEWF, ">>$newfile") or die " Cannot create file $newfile\n";
-
binmode(NEWF);
-
binmode(FILE);
-
-
while( <FILE>){
-
s/^export/set/;
-
if (not /TOOL/){
-
s/"//g;
-
}else{
-
s/""/"/g;
-
}
-
print $_;
-
print NEWF $_;
-
}
-
close(FILE);
-
close(NEWF);
-
}
-
-
replace_func();
binmode
函数把文件转成二进制文件。这样就不会出现windows 操作符 ^M.A filehandle
open for writing or appending may be used with print or printf, appearing
immediately after the keyword but before the list of arguments:
print LOG "Captain's log, stardate 3.14159\n"; # output goes to LOG
printf STDERR "%d percent complete.\n", $done/$total * 100;
Did you notice that there’s no comma between the filehandle and the items to be prin-
ted?* This looks especially weird if you use parentheses. Either of these forms is correct:
printf (STDERR "%d percent complete.\n", $done/$total * 100);
printf STDERR ("%d percent complete.\n", $done/$total * 100);
阅读(875) | 评论(0) | 转发(0) |