Yes, actually there are quite a few good reasons to not do that.

open( LOG, ">>", "log.txt" ) or die "Can't' write to log.txt: $!\n"; *STDOUT= *LOG; print "Yay! This goes to log.txt! Success!\n"; # Reason 1: system("echo Boo, this goes to the old STDOUT"); # Reason 2: close STDOUT; print LOG "Boo, this goes nowhere but I didn't close *LOG!\n"; # Reason 3: open STDOUT, '>>', 'newlog.txt' or die "Can't write to newlog.txt: $!\n"; system("echo Boo, this still goes to old STDOUT despite reopening");

*STDOUT = *LOG makes STDOUT and LOG share the same file handle in a way that is problematic to sane lifespan control. It also hides the old file handle for STDOUT away in a dark place where it will still get used by all manner of things that correctly know that FD 1 is how you send "standard output". And since FD 1 is kept open by this hidden-away, still-open file handle, open's careful work to try to make "open STDOUT" and "open STDERR" work correctly (ie. to have the first impact FD 1 and the second impact FD 2) is forever thwarted.

Just don't manipulate file handles by local()izing and/or overwriting perl globs. Lots of pain to be had there. And this goes doubly when dealing with the STD* file handles.

- tye        


In reply to Re: how do i redirect STDOUT, STDIN, or STDERR to a FILE? (*A=*B sucks) by tye
in thread how do i redirect STDOUT, STDIN, or STDERR to a FILE? by Ectaris

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.