Next | Tricks of the Wizards | 70 |
For example, suppose you'd like to trap all STDOUT output in a file
But also send it to STDOUT as usual
package TeeSTDOUT;
sub import { my ($package, @outfiles) = @_; open REAL_STDOUT, ">&STDOUT" or die ...; my @handles; for my $outfile (@outfiles) { open my $fh, ">", $outfile or die ...; push @handles, $fh; } tie *STDOUT => 'TeeSTDOUT', \@handles; }
sub TIEHANDLE { my ($package, $fhs) = @_; bless $fhs => $package; }
sub PRINT { my ($fhs, $string) = @_; for my $outhandle (@$fhs, \*REAL_STDOUT) { print $outhandle $string; } }
Next | Copyright © 2003 M. J. Dominus |