传递文件句柄并从perl中读取

Nir*_*iru 0 perl

我试图打开一个文件并将其传递给一个函数并读取100行的块.要做到这一点,我做了:

open my $fh, '<', $file or die "Unable to open the file: $!";
#get the header out
my $header = <$fh>;
my @columns =  get_column_headers($header);
getData($fh, 100);

...

sub getData {
  my $fh = shift;
  my $maxLines = shift;
  my $count = 0;
  while (my $line = <$fh> && $count < $maxLines) {
        print "line is : $line \n"; 
  }
}
Run Code Online (Sandbox Code Playgroud)

这个打印行是:1
如果我在打开后在$ fh上打印参考,当我将它传递给getData时,它打印出GLOB.我如何实际检索剩余的行而不是"1",我假设它是读取的行数?我究竟做错了什么?

ike*_*ami 5

只是一个优先问题.

my $line = <$fh> && $count < $maxLines
Run Code Online (Sandbox Code Playgroud)

手段

my $line = ( <$fh> && $count < $maxLines )
Run Code Online (Sandbox Code Playgroud)

所以加上parens

( my $line = <$fh> ) && $count < $maxLines
Run Code Online (Sandbox Code Playgroud)

哦,你忘了增加$count.

( my $line = <$fh> ) && ++$count <= $maxLines
Run Code Online (Sandbox Code Playgroud)