tee’ing perl output

I may want to output info both to screen and sometime to a log file. Perl Cookbook recipe 13-9 offers one approach with Tie::Tee.

I also found a thread at http://perl.org.il/pipermail/perl/2003-June/002302.html where Mark Dominus offered:

my $teefile = "/tmp/teefile";
open my $fh, ">", $teefile or die $!;
open SAVE_STDERR, ">&STDERR" or die $!;
tie *STDERR, 'TEE', *SAVE_STDERR, $fh or die $!;

die "I like pie.n";

package TEE;

my $package = shift;
my @handles = @_;
bless @handles => $package;

sub PRINT {
my ($self, $data) = @_;
for my $fh (@$self) {
print $fh $data;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s