in reply to Re: Redirect STDOUT to a $variable
in thread Redirect STDOUT to a $variable

Rather than specifically closing STDOUT I opened a new file handle and selected it

use strict; print "1: to STDOUT.\n"; my $output = ''; open TOOUTPUT, '>', \$output or die "Can't open TOOUTPUT: $!"; print "2: to STDOUT.\n"; print TOOUTPUT "3: to TOOUTPUT.\n"; select TOOUTPUT; print "4: To STDOUT, (really TOOUTPUT though).\n"; select STDOUT; print "5: To STDOUT again\n"; print TOOUTPUT "6: To TOOUTPUT.\n"; print "----\n".$output."-----\n"; print "Now we're done.\n";
This is the results
1: to STDOUT. 2: to STDOUT. 5: To STDOUT again ---- 3: to TOOUTPUT. 4: To STDOUT, (really TOOUTPUT though). 6: To TOOUTPUT. ----- Now we're done.

Replies are listed 'Best First'.
Re^3: Redirect STDOUT to a $variable
by Anonymous Monk on Jan 26, 2010 at 14:13 UTC
    Thank you very much, you helped a lot!