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

Dear wise monks!

perldoc -f open explains:

In the 2-arguments (and 1-argument) form opening '-' opens STDIN and opening '>-' opens STDOUT.

I try following code (for perl-5.8.0) but do not succeed:

$|=1; my $stdout=''; close STDOUT; open STDOUT, ">", \$stdout; print "this\n"; close STDOUT; open STDOUT, '>-'; print "that\n"; print "[$stdout]";
It does not print anything to STDOUT, while I'm expecting to see output for last two lines.

perl-5.6.1 produces same effect (but it's better to comment a line about opening STDOUT in-memory)
I tried perl on Windows and Linux --- same effect.

What do I do wrong?

Thanks in advance,
Courage, the Cowardly Dog

Replies are listed 'Best First'.
Re: How can I re-open STDOUT?
by broquaint (Abbot) on Aug 07, 2003 at 12:17 UTC
    In the 2-arguments (and 1-argument) form opening '-' opens STDIN and opening '>-' opens STDOUT.
    What that basically does is copy the STDOUT filehandle to the first argument of open e.g
    shell> perl -le 'open( FH => ">-" ); close STDOUT; print FH "test"' shell> perl -le 'open(FH => ">&STDOUT");close STDOUT; print FH "test"' test
    So there you can see that the >- form just copies the STDOUT filehandle, but if it's duplicated then we get the expected behaviour. It would seem that if you close STDOUT you can't open it again (but there's probably some system dependent trickery you can use to get an approximation of it back again).

    Update - further research would indicate that you can't re-open STDOUT at all. This is due to the fact that close STDOUT closes the file descriptor pointed to by STDOUT, and since there isn't a way of natively re-creating STDOUT in perl it's gone for good. However, my research hasn't been exhaustive so if this is inaccurate please let me know.
    HTH

    _________
    broquaint

      broquaint,
      Thanks for the explanation. I had never tried to close and re-open STDOUT without saving it off first, but assumed it was possible. I looked at IO::Handle but then thought if a module could do it - there should be no reason something less heavy couldn't as well. Looking in the Camel on fileno, it has this to say (paraphrased since I am not sure about copyrights):

      File descriptors change between opens and closes, but Perl tries to take care of STDERR, STDOUT, STDIN. Ok, not really - it only pays attention to file descriptors that are less than $^F - whose default is 2. So it all works out. Basically you could reopen STDOUT using open(FILEHANDLE, "<&=$fd") where $fd is the file descriptor number. It goes on to warn that these can change if you start opening and closing them all willy nilly.

      Cheers - L~R

      Update: Per broquaint's /msg to me, this doesn't work. I assumed when the documentation said Perl took special care to look out for certain file descriptors - it meant that it only made it look like you had closed them but it really did the "save so you could restore". Apparently not. After reading perldoc perlvar on $^F - Perl preserves these descriptors during open even in the event of a failure. As we all know - opening up the same filehandle normally closes the original. So if the open fails, you still have STDOUT, and if the open succedes the new handle gets the old descriptor number.

      Update 2: As broquaint and I argued about this for quite some time in the CB, I am going to provide code to illustrate that the docs are right - even though they are misleading. The Camel is even more misleading IMO.

Re: How can I re-open STDOUT?
by Limbic~Region (Chancellor) on Aug 07, 2003 at 12:07 UTC
    Courage,
    OWTDI is to save it off first (also in perldoc -f open):
    #!/usr/bin/perl -w use strict; open (SAVEOUT, ">&STDOUT") or die "Unable to save STDOUT : $!"; open (STDOUT, ">my_log") or die "Error with redirecting STDOUT : $!"; # More code passes open (STDOUT, ">&SAVEOUT") or die "Unable to restore STDOUT : $!";
    Cheers - L~R
      Indeed, I should have to save handle first, and all my code will work afetr that.

      But then, it seems to me that documentation is not clear enough: it states it will *open* STDOUT even if it was closed before.

      Moreover, your example of code does not contains >- anymore.

      Thank you for help!

      Courage, the Cowardly Dog

Re: How can I re-open STDOUT?
by leriksen (Curate) on Aug 08, 2003 at 01:01 UTC
    I may be on completely the wrong track, but IIRC from my C days, 'typically' STDOUT is related intimately with file number 1 (STDIN being 0 and STDERR being 2). When you close and the reopen STDOUT, it does not get the lowest available file number but the next available, which is 3. I believe you have to do a dup call of STDIN to get the next lowest number (thus recreating fileno 1) and then manipulate that (via ioctl's) to recreate it as a clone of STDOUT. How this relates to Perl IO model I have no idea, but I see strong similiarities in the C API and Perl's support

    I feel confident that errors in this assertion will be pointed out to me.