Skip to content

Instantly share code, notes, and snippets.

@wchristian
Created March 18, 2011 10:33
Show Gist options
  • Save wchristian/875874 to your computer and use it in GitHub Desktop.
Save wchristian/875874 to your computer and use it in GitHub Desktop.
package TeeToFile;
use FileHandle;
use PerlIO::Util;
use base 'Exporter';
our @EXPORT_OK = qw( tee_child tee );
sub tee {
my ( $log_file, $code ) = @_;
my $log_fh = FileHandle->new( $log_file, 'a+' );
for ( *STDOUT, *STDERR ) {
$_->autoflush;
$_->push_layer( tee => $log_fh );
}
my @return = $code->();
$_->pop_layer for *STDOUT, *STDERR;
return @return;
}
sub tee_child {
my ( $cmd ) = @_;
open my $MAKE, "$cmd 2>&1 |" or die;
while ( <$MAKE> ) { print }
close $MAKE; # to get $?
return $?;
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment