in reply to how do i redirect STDOUT, STDIN, or STDERR to a FILE?

I found this thread while looking for a way to redirect Data::Dumper output to a file. Using STDOUT as a filehandle can indeed do this:
print "Creating .\\ch9.log ..."; open STDOUT, ">ch9.log" or die $!; print Dumper data_for_path ('.'); close STDOUT; print "Done!\n";
This approach produces an error due to the final print statement (print() on closed filehandle STDOUT at...) Anybody know how I can get STDOUT to direct output back to the screen?

Replies are listed 'Best First'.
Re: Answer: how do i redirect STDOUT, STDIN, or STDERR to a FILE?
by moritz (Cardinal) on Jul 09, 2008 at 22:26 UTC
    No need to open STDOUT in the first place - just selecting a file handle makes it the default target for a print operation:
    open my $handle, '>', 'ch9.log' or die $!; select $handle; print Dumper(...); close $handle; select STDOUT; print "Done!\n";