标题: 新手求助,关于更新文件的问题 [打印本页] 作者: Bruceh2010 时间: 2010-11-09 09:57 标题: 新手求助,关于更新文件的问题 如何去利用内置变量&^I更新文件?
我的代码如下
open(my $file_handle , "update_test.txt" or die "Can not open the file!";#这里我到底应该使用什么样的模式来操作文件?
chomp(my $date = localtime);
$^I =".bak";#这个内置变量的用法到底应该怎么使用
while(<$file_handle>{
s/^Author:.*/Author: Randal L. Scharwartz/;
s/^Phone:.*\n//;
s/^Date:.*/Date: $date/;
print;
}
########################
书上说
“啊,我看到了发生的改变。Perl 修改我的文件fred03.dat,做了我希望的修改,
并将早期的文件保存在叫做fred03.dat.bak 的文件之中”但我们知道的真相是:Perl 不会修改任何文件。它新建了一份修改
后的拷贝,说“Abracadabra(咒语)”,当在魔术棒出现过闪光后,文件就被交换了。很狡猾吧!
意思我明白,但是到底是怎么工作的啊,help me~!作者: Bruceh2010 时间: 2010-11-09 09:59
人工置顶一下:wink:作者: zhlong8 时间: 2010-11-09 10:54
楼主难道是要这个?
perl -i.bak -pe "s/^Author:.*/Author: Randal L. Scharwartz/;s/^Phone:.*\n//;s/^Date:.*/Date: $date/;" testfile
复制代码
作者: zhlong8 时间: 2010-11-09 11:02
-i[extension]
specifies that files processed by the <> construct are to be edited in-place. It does this by renaming the input file, opening the output file by the original name, and selecting that output file as the default for print() statements. The extension, if supplied, is used to modify the name of the old file to make a backup copy,