Perl File Handle Problem
Hi,
I have a script in which i need to capture everything written to STDOUT. I have managed to redirect output from STDOUT to $varaible. But I cant switch STDOUT back. How can I do this. Originally I did this my $out = new IO::Scalar \$variable; my $stdout = select $out; ... close select $stdout; However one of the packages I was using now does print STDOUT where it used to just print; Any ideas?? Cheers |
Take a look at the example in
man perlfunc under the function "open". |
Thanks, however...
I've read it over and over. It shows you how to store STDOUT in a variable and how to reopen it to a scalar. But it doesnt show how to reassign STDOUT from the saved variable, so that normal output is resumed. |
In my man page, it explains that (summarized):
open $oldout, ">&STDOUT" #opens STDOUT in $oldout variable open STDOUT, '>', "foo.out" #redirects STDOUT to "foo.out" (write mode) open STDOUT, ">&", $oldout #redirect STDOUT once more, this time to $oldout, where the original stdout was #stored in. So this should effectively restore the normal STDOUT. Have you tried that? And added some prints to test if all is well? |
Thanks,
A couple of differneecs: I was doing : my $stdout = *STDOUT; Instead of: open $oldout, ">&STDOUT"; Needed a close(STDOUT), but then worked fine. Cheers |
I suppose you were looking at
man perldata in the section on filehandles and wrote my $stdout = *STDOUT; based on that? Have you also read man perlfaq5 ? Functions like "fdopen", "fdclose" (close filehandles based on their number - I think STDOUT is number 1) can be helpful too. Finally it's also possible to "dup" (duplicate) or alias a filehandle. man perlfaq5 explains how this works, at least for STDIN. |
All times are GMT -5. The time now is 02:31 PM. |