in reply to how to send both STDOUT and STERR to the same file
use IO::Handle; my $file = 'output.dat'; open(OUTPUT, ">$file") || die $!; STDOUT->fdopen(\*OUTPUT, "w") || die $!; open STDERR, ">&STDOUT"; print "This is STDOUT\n"; print STDERR "This is STDERR\n"; warn "This is a warning\n";
Update: Cleaner:
my $file = 'output.dat'; { open(my $fh_out, '>', $file) or die("Unable to create output file \"$file\": $!\n"); local *STDOUT = $fh_out; local *STDERR = $fh_out; print "This is STDOUT (1)\n"; print STDERR "This is STDERR (1)\n"; warn "This is a warning (1)\n"; } print "This is STDOUT (2)\n"; print STDERR "This is STDERR (2)\n"; warn "This is a warning (2)\n";
If you want child processes to inherit STDOUT and STDERR:
use v5.8.0; my $file = 'output.dat'; { open(my $fh_out, '>', $file) or die("Unable to create output file \"$file\": $!\n"); local *STDOUT; open(*STDOUT, '>&', $fh_out) or die("Unable to redirect STDOUT: $!\n"); local *STDERR; open(*STDERR, '>&', $fh_out) or die("Unable to redirect STDERR: $!\n"); print "This is STDOUT (1)\n"; print STDERR "This is STDERR (1)\n"; warn "This is a warning (1)\n"; } print "This is STDOUT (2)\n"; print STDERR "This is STDERR (2)\n"; warn "This is a warning (2)\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: how to send both STDOUT and STERR to the same file
by Anonymous Monk on Oct 19, 2006 at 17:51 UTC |