perl文件句柄的传递

perl 返回文件句柄的2种方式

1.使用 \*

#!/usr/bin/perl
use strict;
sub openfile()
{
        
    my $path=shift;
    open(FILE,"$path") or die "Can't open $path $!\n ";
    
  return \*FILE;
    
}
my $temp=&openfile("config");
my @file=<$temp>;
print @file;

 

2.使用变量

#!/usr/bin/perl
use strict;
sub openfile()
{
    my $file;
    my $path=shift;
    open($file,"$path") or die "Can't open $path $!\n ";
    
    #close($file);
    return $file;
    
}
my $temp=&openfile("config");
my @file=<$temp>;
print @file;

 

posted @ 2013-08-01 23:43  to be crazy  阅读(3924)  评论(0编辑  收藏  举报