免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1930 | 回复: 6
打印 上一主题 下一主题

请教:"<>"不需要open一个文件就可以直接从文件读取? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-01-19 10:06 |只看该作者 |倒序浏览
下面这段代码取自TAHI的IPV6测试工具,autorun文件

@INDEX=@ARGV;

parseIndex();

sub parseIndex(){
        my $testnum=1;
        my $num;
        for($num=1;<>;$num++) {
                chomp;
                $status[$num]{type}="";
.....
}

其中上面的ARGV是几个文件名:INDEX_ND_p1_router  INDEX_RD_p1_router  INDEX_REDIRECT_p1_router
让我疑惑的是在parseIndex这个函数中是怎么打开这三个文件并一行一行读取这三个文件中的内容的?是不是for循环当中的这个“<>”?
如果是,那么这个“<>”是如何取得这三个文件的文件句柄的?
如果不是,那是怎么打开这三个文件的?

本人初学PERL,问题初浅,请见谅!
谢谢各位的指教!

注:可以肯定是在这个parseIndex函数中打开了这三个文件,因为其中的一些正则表达式就是对这三个文件的解析,而且运行的结果也说明了这一点。

论坛徽章:
0
2 [报告]
发表于 2007-01-19 10:13 |只看该作者
原帖由 universes 于 2007-1-19 10:06 发表
下面这段代码取自TAHI的IPV6测试工具,autorun文件

@INDEX=@ARGV;

parseIndex();

sub parseIndex(){
        my $testnum=1;
        my $num;
        for($num=1;<>;$num++) {
                chomp;
                $status[$num]{type}=& ...


这个不奇怪啊,你自己test下就知道了:

#!/usr/bin/perl

while(<>) {
    print;
}

然后执行./test.pl a.txt
会把a.txt里的内容print出来.

<>这里期待STDIN的数据,如果参数跟了文件名,就把文件内容作为STDIN输入了.

for($i=0;<>;$++)这里表示逐行读取文件内容,并将$i累加.

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
3 [报告]
发表于 2007-01-19 10:15 |只看该作者
没错,就是 <>
详情请看 perldoc perlop
  1. The null filehandle <> is special: it can be used to emulate the
  2. behavior of sed and awk. Input from <> comes either from standard input,
  3. or from each file listed on the command line. Here's how it works: the
  4. first time <> is evaluated, the @ARGV array is checked, and if it is
  5. empty, $ARGV[0] is set to "-", which when opened gives you standard
  6. input. The @ARGV array is then processed as a list of filenames. The
  7. loop

  8.     while (<>) {
  9.         ...                     # code for each line
  10.     }

  11. is equivalent to the following Perl-like pseudo code:

  12.     unshift(@ARGV, '-') unless @ARGV;
  13.     while ($ARGV = shift) {
  14.         open(ARGV, $ARGV);
  15.         while (<ARGV>) {
  16.             ...         # code for each line
  17.         }
  18.     }

  19. except that it isn't so cumbersome to say, and will actually work. It
  20. really does shift the @ARGV array and put the current filename into the
  21. $ARGV variable. It also uses filehandle *ARGV* internally--<> is just a
  22. synonym for <ARGV>, which is magical. (The pseudo code above doesn't
  23. work because it treats <ARGV> as non-magical.)

  24. You can modify @ARGV before the first <> as long as the array ends up
  25. containing the list of filenames you really want. Line numbers ($.)
  26. continue as though the input were one big happy file. See the example in
  27. "eof" in perlfunc for how to reset line numbers on each file.
复制代码

论坛徽章:
0
4 [报告]
发表于 2007-01-19 10:38 |只看该作者
谢谢两位的指点!
还有一点小问题:
autorun INDEX_ND_p1_router  INDEX_RD_p1_router  INDEX_REDIRECT_p1_router
此时ARGV就是这三个文件名了。
这个ARGV是不是相当于一个全局变量?传到主流程里了,然后在parseIndex里的“<>”就直接读取这个全局变量ARGV了?
如果parseIndex调用另一个函数sub test,在test中也可以读取这个ARGV并且值还是“INDEX_ND_p1_router  INDEX_RD_p1_router  INDEX_REDIRECT_p1_router”(如果ARGV没被修改的话)?

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
5 [报告]
发表于 2007-01-19 10:40 |只看该作者
完全正确。
@ARGV 就是全局变量,不过 <> 会修改它——每读完一个文件,@ARGV 里就会少一个文件。

论坛徽章:
0
6 [报告]
发表于 2007-01-19 10:50 |只看该作者
<>缺省从输入参数文件中读取,如果没有输入参数,会从标准输入读取
autorun INDEX_ND_p1_router
和autorun < INDEX_ND_p1_router 效果一样。不过文件多还是用参数。

论坛徽章:
0
7 [报告]
发表于 2007-01-19 10:51 |只看该作者
非常感谢各位的指点!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP