楼上的方法是对的,perl读写的句柄不能混用,无法在原地做替换,必须输出另外一个文件。
如果是linux系统,原地替换可以用sed -i命令~~~~~
#! /usr/bin/perl -w
# changing all of 'adf' of every line within the file named 123 of the current directory to 'AWW'.
open( _123_, "<", "./123" ) or die "123 didn't exist in the current directory !\n";
open( _123_BAK_, ">", "./123.bak" ) or die "123.bak didn't exist in the current directory !\n";
while ( <_123_> ) {
chomp;
if ( /\badf\b/i ) {
s/adf/AWW/g;
}
print _123_BAK_ $_ . "\n";
}
close( _123_ );
close( _123_BAK_ );
system "mv ./123.bak ./123";
system "rm -rf ./123.bak";