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";
In reply to Re: how to send both STDOUT and STERR to the same file
by ikegami
in thread how to send both STDOUT and STERR to the same file
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |