ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Filehandle References

Filehandle References

原创 Linux操作系统 作者:物理狂人 时间:2012-05-03 10:55:35 0 删除 编辑
1)IO::File
use IO::File;

my $fh = IO::File->new( ' > castaways.log' )
or die "Could not create filehandle: $!";

my $read_fh = IO::File->new( 'castaways.log', 'r' );

my $write_fh = IO::File->new( 'castaways.log', 'w' );

my $append_fh = IO::File->new( 'castaways.log', O_WRONLY|O_APPEND);

my temp_fh = IO::File->new_tmpfile;

$temp_fh->close;

undef $append_fh;

2) Anonymous IO::file Objects
my @handlepairs;

foreach my $file (('disk.txt','file.txt')){
(my $out = $file) =~ s/\.txt$/.output/;
push @handlepairs, [
(IO::File->new("<$file") || die),
(IO::File->new(">$out") || die),
];
};

while (@handlepairs) {
@handlepairs = grep {
if (defined(my $line = $_->[0]->getline)){
print {$_->[1]} $line;
}else{
0;
}
} @handlepairs;
}
并行执行

3)IO::Scalar
ppm install IO::Scalar

use IO::Scalar;

my $string_log = '';
my $scalar_fh = IO::Scalar->new(\$string_log);

print $scalar_fh "The Howells' private beach club is closed\n"

use IO::Scalar;

my $string_log ='';
my $scalar_fh = IO::Scalar->new(\$string_log);

while(<$scalar_fh>){
next unless /Gilligan/;
print;
}

version 5.8
open (my $fh, '>>', \$string_log)
or die "Could not append to string! $!";

4)IO::Tee
use IO::Tee;

open my $log_fh, '>>', 'castaways.log'
or die "Could not open castaways.log";
open my $scalar_fh, '>>', \$string;
$tee_fh = IO::Tee->new($log_fh, $scalar_fh);

print $tee_fh "The radio works in the middle of the ocean!\n";

如果iO::Tee第一个参数是输入,那么后续参数必须是输出

use IO::Tee;

$tee_fh = IO::Tee->($read_fh, $log_fh, $scalar_fh);

#read from $read_fh
my $message = <$tee_fh>;

#print to $log_fh and $scalar_fh
print $tee_fh $message;


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/24104518/viewspace-722609/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2010-06-16

  • 博文量
    80
  • 访问量
    142087