亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1975 | 回復(fù): 6
打印 上一主題 下一主題

請教:"<>"不需要open一個文件就可以直接從文件讀取? [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 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這個函數(shù)中是怎么打開這三個文件并一行一行讀取這三個文件中的內(nèi)容的?是不是for循環(huán)當中的這個“<>”?
如果是,那么這個“<>”是如何取得這三個文件的文件句柄的?
如果不是,那是怎么打開這三個文件的?

本人初學PERL,問題初淺,請見諒!
謝謝各位的指教!

注:可以肯定是在這個parseIndex函數(shù)中打開了這三個文件,因為其中的一些正則表達式就是對這三個文件的解析,而且運行的結(jié)果也說明了這一點。

論壇徽章:
0
2 [報告]
發(fā)表于 2007-01-19 10:13 |只看該作者
原帖由 universes 于 2007-1-19 10:06 發(fā)表
下面這段代碼取自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;
}

然后執(zhí)行./test.pl a.txt
會把a.txt里的內(nèi)容print出來.

<>這里期待STDIN的數(shù)據(jù),如果參數(shù)跟了文件名,就把文件內(nèi)容作為STDIN輸入了.

for($i=0;<>;$++)這里表示逐行讀取文件內(nèi)容,并將$i累加.

論壇徽章:
1
2015年辭舊歲徽章
日期:2015-03-03 16:54:15
3 [報告]
發(fā)表于 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.
復(fù)制代碼

論壇徽章:
0
4 [報告]
發(fā)表于 2007-01-19 10:38 |只看該作者
謝謝兩位的指點!
還有一點小問題:
autorun INDEX_ND_p1_router  INDEX_RD_p1_router  INDEX_REDIRECT_p1_router
此時ARGV就是這三個文件名了。
這個ARGV是不是相當于一個全局變量?傳到主流程里了,然后在parseIndex里的“<>”就直接讀取這個全局變量ARGV了?
如果parseIndex調(diào)用另一個函數(shù)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 [報告]
發(fā)表于 2007-01-19 10:40 |只看該作者
完全正確。
@ARGV 就是全局變量,不過 <> 會修改它——每讀完一個文件,@ARGV 里就會少一個文件。

論壇徽章:
0
6 [報告]
發(fā)表于 2007-01-19 10:50 |只看該作者
<>缺省從輸入?yún)?shù)文件中讀取,如果沒有輸入?yún)?shù),會從標準輸入讀取
autorun INDEX_ND_p1_router
和autorun < INDEX_ND_p1_router 效果一樣。不過文件多還是用參數(shù)。

論壇徽章:
0
7 [報告]
發(fā)表于 2007-01-19 10:51 |只看該作者
非常感謝各位的指點!
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP