Another option, which I have never used, is Tie::FileHandle::MultiPlex.
... and if you hunt around CPAN, you'll also find Filter::tee and IO::Tee, which look to be of a similar vein.
However, if you want to avoid modules or a dependency on an external (and potentially not portable!) binary, you can acheive tee-like functionality in Perl with the following snippet (adapated from The Perl Cookbook 16.5) ...
sub tee { my $file = shift; open FILE, "> $file" or die "Cannot open($file) for writing: $!\n"; return if my $pid = open STDOUT, "|-"; die "Cannot fork STDOUT: $!\n" unless defined $pid; print FILE while <STDIN>; close FILE; exit; } tee('/some/file'); while ( <> ) { print; } close STDOUT; # be nice to tee's
Update: D'oh... shoulda tested the code in the <readmore> before posting. Although it prolly still could be made to work, ybiC's code is simpler.
--k.
In reply to Re: Re: printing to STDOUT and a Logfile
by Kanji
in thread printing to STDOUT and a Logfile
by oroburos
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |