如何判断Perl中的文件句柄是否为空?

joe*_*joe 0 perl filehandle

例如:

open (PS , " tail -n 1 $file | grep win " );
Run Code Online (Sandbox Code Playgroud)

我想查找文件句柄是否为空.

FMc*_*FMc 5

您还可以使用eof检查文件句柄是否已用尽.这是一个基于您的代码的插图.还要注意使用带有3-arg形式的词法文件句柄open.

use strict;
use warnings;

my ($file_name, $find, $n) = @ARGV;

open my $fh, '-|', "tail -n $n $file_name | grep $find" or die $!;

if (eof $fh){
    print "No lines\n";
}
else {
    print <$fh>;
}
Run Code Online (Sandbox Code Playgroud)