Perl: redirecting STDERR to STDOUT

I had occasion that I needed to redirect all STDERR output (die statements) to STDOUT. A webapp I wrote only looks at STDOUT, so this code causes STDERR to merge with STDOUT.

open(STDERR, ">&STDOUT");

Alternately you can also do it by reassigning the raw file handles:


You can get fancy and redirect all output to a log file:


Leave A Reply - 1 Reply
Beli 2011-07-19 01:55am - - Logged IP:

Thank you for the examples. I'd just add that the first alternative: open(STDERR, ">&STDOUT"); affects also the output of executed programs, e.g. with system(), while the second one: STDERR = STDOUT; affects only output generated by your script, not other processes executed from within. Best regards. Beli

All content licensed under the Creative Commons License