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/