JFarr has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks

I've been able to chagne the STDOUT with the tee.pl script, so I can write to the console and out output file.

Now to refine it I want to be able to change from writing to the file and console, to just the console. Because we do not need to log everything we encounter. I've been beating my head against the wall today, trying different things but have not had any success. I figured someone must have done this already.

Here is the code I have used to change STDOUT.
open( $stdout , ">&STDOUT" ); open( $stderr , ">&STDERR" ); open( STDOUT , "| perl tee.pl -a $fErrorLogFile" ); select STDOUT; $| = 1; ... code ... print "Some text....\n";
The above snippet works fine.
But how do I change back to normal STDOUT, where I am not writing to a file also?

Many thanks in advance.

Replies are listed 'Best First'.
Re: changing STDOUT
by ikegami (Patriarch) on Jul 21, 2006 at 22:35 UTC

    You should always constrain your variables to a scope as small as possible. Typically, one would use my, but globs require the use of local. Had you done this here, STDOUT would have restored itself.

    { open(local *STDOUT , "| perl tee.pl -a $fErrorLogFile" ); $| = 1; ... } print "Some text....\n";
Re: changing STDOUT
by planetscape (Chancellor) on Jul 22, 2006 at 02:11 UTC
Re: changing STDOUT
by Leviathan (Scribe) on Jul 22, 2006 at 13:35 UTC

    You already backed up STDOUT in $stdout. So all you need to do to print to STDOUT only is:

    open( $stdout , ">&STDOUT" ); open( $stderr , ">&STDERR" ); open( STDOUT , "| perl tee.pl -a $fErrorLogFile" ); select STDOUT; $| = 1; print "Some text....\n"; .. .. code .. .. select $stdout; print "Some other text....\n";

    Note that you might have some buffering problems here.

    --
    Leviathan