Chinaunix

标题: 新手求助,关于更新文件的问题 [打印本页]

作者: 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
楼主难道是要这个?
  1. perl -i.bak -pe "s/^Author:.*/Author: Randal L. Scharwartz/;s/^Phone:.*\n//;s/^Date:.*/Date: $date/;" testfile
复制代码

作者: zhlong8    时间: 2010-11-09 11:02
  1. -i[extension]
  2. 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,
复制代码
perldoc perlrun
作者: Bruceh2010    时间: 2010-11-09 11:04
回复 3# zhlong8

这是命令行的形式吧,我主要问题是没弄明白这个内置变量如何使用和工作的,谢谢你.
作者: zhlong8    时间: 2010-11-09 11:09
回复 5# Bruceh2010


    上面文档说的很清楚,只对 <> 这个 magic 才起作用
作者: Bruceh2010    时间: 2010-11-09 11:13
回复  Bruceh2010


    上面文档说的很清楚,只对  这个 magic 才起作用
zhlong8 发表于 2010-11-09 11:09

我明白了,<>里是不能有参数的对么
作者: zhlong8    时间: 2010-11-09 11:18
只能用默认值,至少文档是这个意思。目前就找到 perlrun 中的 -i 和 perlvar 中的 $^I 看这两个地方应该是不能在里面加参数了。
作者: Bruceh2010    时间: 2010-11-09 11:19
本帖最后由 Bruceh2010 于 2010-11-09 11:20 编辑

看到了网上有人说使用tie模块来操作文件,我试了一下文件更新成功.
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use Tie::File;
  4. =pod
  5. 使用tie 模块来处理文件修改问题
  6. =cut
  7. my @lines;
  8. my $filename="update_test.txt";#指定要操作的文件
  9. my $date = localtime;
  10. tie(@lines ,'Tie::File',$filename) or die $!;
  11. foreach my $i (@lines){
  12.     if($i =~ /\n/){#这里匹配不上.
  13.         print "got it";
  14.     }
  15.     $i =~ s/Phone.*//;
  16.     $i =~s/^Author:.*/Author: Randal L. Scharwartz/;
  17.     $i =~s/^Date:.*/Date: $date/;
  18.     print "$i\n";
  19. }
  20. untie(@lines);
复制代码
但是我发现一个问题,为什么我匹配不上换行呢?
作者: zhlong8    时间: 2010-11-09 11:21
本帖最后由 zhlong8 于 2010-11-09 11:28 编辑

回复 9# Bruceh2010

tie 背后为你做了很多小动作你才能像数组一样使用文件。

\n 应该可以匹配得上的啊,你检查下是不是别的地方有错?
作者: zhlong8    时间: 2010-11-09 11:28
回复 9# Bruceh2010


    用之前先看文档 Tie::File
Records read from the tied array do not have the record separator string on the end;

作者: Bruceh2010    时间: 2010-11-09 11:32
回复  Bruceh2010


    用之前先看文档 Tie::File
zhlong8 发表于 2010-11-09 11:28

tie  给我的字符数组没有\n 是已经被去掉了吧,忘记看文档了..3q
作者: Bruceh2010    时间: 2010-11-09 11:35
回复  Bruceh2010


    用之前先看文档 Tie::File
zhlong8 发表于 2010-11-09 11:28


文档都是英文的,简单的点儿的还行,有时候有些单词的意思,跟平时的相差很多,就比较难理解了.
作者: zhlong8    时间: 2010-11-09 11:38
本帖最后由 zhlong8 于 2010-11-09 11:42 编辑

回复 13# Bruceh2010


    计算机英语都挺简单的,专用的名词也就那么些,耐心看完一本计算机方面的原版书应该就差不多了。看完之后 官方文档一般小意思啦
作者: Bruceh2010    时间: 2010-11-09 11:43
回复  Bruceh2010


    计算机英语都挺简单的,专用的名词也就那么些,耐心看完一本计算机方面的原版书 ...
zhlong8 发表于 2010-11-09 11:38

my god...  原版书...:wink:
作者: zhlong8    时间: 2010-11-09 11:58
回复 15# Bruceh2010


    150页口水+150页代码的小骆驼就是很好的原版书,以楼主的水平不至于连150页的书都看不下来吧。
作者: Bruceh2010    时间: 2010-11-09 12:34
回复  Bruceh2010


    150页口水+150页代码的小骆驼就是很好的原版书,以楼主的水平不至于连150页的书 ...
zhlong8 发表于 2010-11-09 11:58

我来试一试吧.
作者: ioerr    时间: 2012-06-21 10:52
tie::file修改文件,学习了,thx




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2