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.

#!/usr/bin/perl -w use strict; print "foo bar\n"; # Goes to screen open (STDOUT, ">my_log") or die $!; print "foo bar\n"; # Goes to Log open (STDOUT,">&=1") or die $!; print "foo bar\n"; # Hoped it went to screen, but goes to log
Now notice what happens with close
#!/usr/bin/perl -w use strict; print "foo bar\n"; # Goes to screen open (STDOUT, ">my_log") or die $!; print "foo bar\n"; # Goes to Log close STDOUT; # Gone forever open (STDOUT,">&=1") or die $!; # death - goodbye cruel world
Ok - what happens if the open fails and you don't say or die?
#!/usr/bin/perl -w use strict; print "foo bar\n"; # Goes to screen open (STDOUT, ">/my_log"); # Don't have write permission to / print "foo bar\n"; # Goes to screen since it is preserved

In reply to Re: Re: How can I re-open STDOUT? by Limbic~Region
in thread How can I re-open STDOUT? by Courage

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.