簡體   English   中英

perl進程文件句柄

[英]perl process filehandle

我正在嘗試學習如何使用文件句柄從進程中讀取。 我試圖編寫一個程序,從'date'命令打開一個文件句柄,並使用以下程序按要求進行引用:

 #!/usr/bin/perl
 use warnings;
 use diagnostics;
 use strict;

 open DATE, 'date|' or die "Cannot pipe from date: $!";
 my $now;

 while (1) {
  print "Press any key: ";
  chomp( my $result=<STDIN> );

  $now = <DATE>;

  print "\$now is: $now\n";

 }   

這一次或最初有效,但隨后在while(1)中“循環”,第二次出現錯誤,提示“ $ now的未初始化值”。 更奇怪的是,錯誤在第二個循環之后消失了,但是'date'中的數據從未填充到$ now變量中。

我認為這可能表明文件句柄在第一次運行后就關閉了,但是我不知道是不是這種情況。 我現在正在學習perl,我相信這很容易,而且我對語言沒有足夠的經驗。

有人可以為此提供指導嗎? 我是否應該期望DATE文件句柄在最初通過循環運行后仍保持打開狀態?

這是程序此時的輸出:

 $ perl ./test.perl
 Press any key:
 $now is: Fri Aug 17 15:00:27 EDT 2012

 Press any key:
 Use of uninitialized value $now in concatenation (.) or string at ./test.perl
         line 16, <DATE> line 1 (#1)
     (W uninitialized) An undefined value was used as if it were already
     defined.  It was interpreted as a "" or a 0, but maybe it was a mistake.
     To suppress this warning assign a defined value to your variables.

     To help you figure out what was undefined, perl will try to tell you the
     name of the variable (if any) that was undefined. In some cases it cannot
     do this, so it also tells you what operation you used the undefined value
     in.  Note, however, that perl optimizes your program and the operation
     displayed in the warning may not necessarily appear literally in your
     program.  For example, "that $foo" is usually optimized into "that "
     . $foo, and the warning will refer to the concatenation (.) operator,
     even though there is no . in your program.

 $now is:
 Press any key:
 $now is:
 Press any key:
 $now is:
 Press any key:
 $now is:
 Press any key: ^C     

沒錯, date只產生一個輸出,一旦讀取,緩沖區中就什么也沒有了。 您可以執行以下操作:

while (1) {
  # ...
  open DATE, 'date|' or die "Cannot pipe from date: $!";
  $now = <DATE>;
  close DATE;
  # ...
  print "\$now is: $now\n";

}   

另一種方法是使用反向抽動:

while (1) {
  print "Press any key: ";
  chomp( my $result=<STDIN> );
  print "now is: ".`date`."\n";
 }   

您的程序正在按規定運行。 date命令返回一行,並且您試圖讀取pass到文件末尾

正確的方法是:

#! /usr/bin/env perl
use warnings;
use diagnostics;
use strict;
use autodie;
use feature qw(say);

open my $date, "|-", "date";

while (my $now = <$date>) {
   print "Press any key: ";
   chomp( my $result=<STDIN> );

   say "\$now is: $now";
}

程序將執行一個循環。

一些東西:

  • use feature qw(say); 允許您使用類似於printsay命令,但不必始終將\\n放在最后。
  • use autodie; 自動允許open語句消失而無需測試。 如果您無論如何都會死於open ,那么為什么不讓Perl為您做呢。
  • 最好使用三參數open命令。 並且,也可以將標量用於文件句柄。

您可能想要的是這樣的東西:

#! /usr/bin/env perl
use warnings;
use diagnostics;
use strict;
use autodie;
use feature qw(say);

for(;;) {
    print "Press Return to continue...";
    <STDIN>;
    chomp ( my $date = qx(date) );
    say qq(\$date = "$date");
}
  • qx(...)表示執行引號 ,而不是反引號。
  • qq(...)是雙引號字符串的另一種方法。 這使您可以使用引號而不用反斜杠引號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM