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

Dear Monks

I have a script that at some point redirects STDOUT to a file
open(STDOUT, ">my_file") ; select STDOUT; $| = 1;
But at some point I would like to close the file and from that moment on I would like to print everything to screen. I tried
close STDOUT ; open STDOUT,">&1" or die "Cannot do it" ;
And it doesn't work. Probably &1 and STDOUT are the same thing.
Any suggestion what I should do to fix this ?

Thnx
LuCa

Replies are listed 'Best First'.
Re: reuse STDOUT after it was closed
by davorg (Chancellor) on Mar 12, 2007 at 11:12 UTC

    The documentation for "open" has some sample code that "saves, redirects, and restores STDOUT and STDERR using various methods".

    #!/usr/bin/perl open my $oldout, ">&STDOUT" or die "Can't dup STDOUT: $!"; open OLDERR, ">&", \*STDERR or die "Can't dup STDERR: $!"; open STDOUT, '>', "foo.out" or die "Can't redirect STDOUT: $!"; open STDERR, ">&STDOUT" or die "Can't dup STDOUT: $!"; select STDERR; $| = 1; # make unbuffered select STDOUT; $| = 1; # make unbuffered print STDOUT "stdout 1\n"; # this works for print STDERR "stderr 1\n"; # subprocesses too open STDOUT, ">&", $oldout or die "Can't dup \$oldout: $!"; open STDERR, ">&OLDERR" or die "Can't dup OLDERR: $!"; print STDOUT "stdout 2\n"; print STDERR "stderr 2\n";

      Just wondering (spend too much time on a 'doz box), but would reopening STDOUT to /dev/tty work?

        The difference between restoring STDOUT and re-opening it to /dev/tty is the case where STDOUT wasn't originally connected to the user's terminal.

        For example, if the output was redirected to a file by the user starting the program, if STDOUT were restored any output after it was restored would go to the file; if it were re-opened to /dev/tty output would start out going to the file, but after STDOUT was re-opened further output would go to the terminal.

        It's not completely clear from the OPs question which behavior is desired, so it's quite possible opening /dev/tty is the right solution.

Re: reuse STDOUT after it was closed
by rodion (Chaplain) on Mar 12, 2007 at 10:54 UTC
    Depending on the structure of your code, you could use somehting as simple as localizing STDOUT.
    print "Msg to original STDOUT\n"; { local *STDOUT; open(STDOUT, ">my_file") ; select STDOUT; $| = 1; print "msg to Redirected STDOUT\n"; } print "Msg to original STDOUT again\n";
    Note that the localized copy of STDOUT will be used by any subs called from within the block (after the local), unlike with "my", but from your post, I think that's what you want. (Localizing STDOUT when you change it is good practice in any case.)
Re: reuse STDOUT after it was closed
by shigetsu (Hermit) on Mar 12, 2007 at 14:50 UTC
Re: reuse STDOUT after it was closed
by jeanluca (Deacon) on Mar 13, 2007 at 15:25 UTC
    Thnx monks, thats all I needed to know. The solution suggested by davorg is probably the easiest one for me!

    LuCa