在perl的函数定义中,如果使用文件句柄作为参数进行传递,请使用引用,而不要使用直接的变量赋值操作。原因是如果在其它包中引用这个函数,将导致文件句柄非法错误。
use 5.006;
use strict;
use warnings;
sub read_file {
my $h = shift;
my @lines = <$h>;
print @lines;
}
open(FH,"sub_read_file.pl") or die "can not open sub_read_file:$!/n";
my $handle = /*FH;
read_file($handle);
close(FH);