LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Perl File Handle Problem (https://www.linuxquestions.org/questions/linux-software-2/perl-file-handle-problem-413453/)

barkers 02-09-2006 09:43 AM

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

timmeke 02-10-2006 02:20 AM

Take a look at the example in
man perlfunc
under the function "open".

barkers 02-10-2006 03:06 AM

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.

timmeke 02-10-2006 03:30 AM

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?

barkers 02-10-2006 06:43 AM

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

timmeke 02-10-2006 08:00 AM

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.