Filehandle References

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("
(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(){
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 = ;

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


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

转载于:http://blog.itpub.net/24104518/viewspace-722609/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值