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

How can I Undo a STDOUT redirect so that my print statements go back to printing to the windows console?
open(STDOUT, "> $LogFile"); select(STDOUT); $|=1; print "Something" #Now I want to undo my redirect...and print to the console window.

Replies are listed 'Best First'.
Re: Undo a STDOUT redirect
by dtr (Scribe) on Oct 19, 2005 at 20:38 UTC

    Localise the change to STDOUT. EG:-

    { local *STDOUT; open(STDOUT, "> $LogFile") || die "Failed: $!"; print STDOUT "Blah"; } print "This should appear on the console window again";

    However, I do have to ask - why use STDOUT for this as opposed to another filehandle?

    Update: Aaarrrggghhh. Beaten to it by BUU. Apologies for the duplicate response.

Re: Undo a STDOUT redirect
by BUU (Prior) on Oct 19, 2005 at 20:36 UTC
    perhaps:
    print "stdout"; { local *STDOUT; open STDOUT, "> $logfile" or die $!; print "yo"; } print "stdout2";
Re: Undo a STDOUT redirect
by chester (Hermit) on Oct 19, 2005 at 20:46 UTC
    From perldoc open:

    use warnings; use strict; open my $old, ">&", "STDOUT" or die $!; open STDOUT, ">", "test" or die $!; print "To file...\n"; close STDOUT or die $!; open STDOUT, ">&", $old or die $!; print "To console...\n";
Re: Undo a STDOUT redirect
by dimishome (Beadle) on Oct 19, 2005 at 20:52 UTC
    What you can do is use the Local::TeeOutput mod. This will allow you to open a connection set with different parameters and then close the connection to refer back to the original, before the open connection, parameters. It would be something like this.
    use Local::TeeOutput; openTee(*STDOUT, ">$LogFile"); select(STDOUT); $|=1; print "Something" closeTee(*STDOUT);
Re: Undo a STDOUT redirect
by Thelonius (Priest) on Oct 20, 2005 at 00:41 UTC
    You could also do this:
    open LOGFILE, ">", $LogFile or die; select(LOGFILE); $|=1; print "Something"; select(STDOUT);
    # and you can close LOGFILE if you're done with it.