Hey everyone-- There's nothing magical about SAVEOUT. I was trying to use Cookbook 7.20 to try a solution and picked an arbitrary filehandle (SAVEOUT) to test stuff out.

I've tried VSarkiss' idea already. The error messages were vague.

I then tried Brent's idea:

#!/usr/bin/perl -w $log_file = "logfile"; open(STDOUT, "| tee $log_file") or die "Can't open: $!\n"; print "Testing 1.2.3...\n"; close(STDOUT) or die "Can't close: $!\n"; open(STDOUT,'>-'); print "done\n"; close(STDOUT);
But that didn't work. I also tried ">-" but that didn't work either...In both cases, I never got "done" printed to the STDOUT. I stumbled upon this solution, though:
#!/usr/bin/perl -w $log_file = "logfile"; #copy file descriptor open(OLDOUT,">&STDOUT"); open(STDOUT, "| tee $log_file") or die "Can't open: $!\n"; print "Testing 1.2.3...\n"; close(STDOUT) or die "Can't close: $!\n"; #restore STDOUT open(STDOUT,">&OLDOUT"); print "done\n"; close(OLDOUT);
Results:
Testing 1.2.3... gets put into logfile and STDOUT done gets put on STDOUT only
Is there a less convoluted way of doing this without having to copy file descriptors? And why does this solution work...it still puzzles me why this works...I somehow got to this solution by reading 7.20.

Thanks everyone for your help. --slojuggler2


In reply to Re: How do I restore STDOUT by slojuggler2
in thread How do I restore STDOUT by slojuggler2

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.