Oops. Thanks for pointing that out. (I should have tested before posting.)

While we're at it (now that I have tried it out), there's another aspect of my snippet that didn't work as I expected: re-opening STDOUT that way does not seem to put things back the way they were:

#!/usr/bin/perl warn "\n==normal case:\n"; print "running ls:\n"; system( "ls" ); close STDOUT; warn "\n==STDOUT closed:\n"; print "running ls a 2nd time\n"; system( "ls" ); open STDOUT, '>-'; warn "\n==STDOUT reopened:\n"; print "running ls a 3rd time\n"; system( "ls" );

When I run that in at terminal shell, the only output I get on STDOUT comes from the "normal case", nothing shows up after the second and third "warn" messages (bummer).

I had to read farther into the manual description of open to get it right:

#!/usr/bin/perl warn "\n==normal case:\n"; print "running ls:\n"; system( "ls" ); open my $oldout, ">&STDOUT"; # "dup" the stdout filehandle close STDOUT; warn "\n==STDOUT closed:\n"; print "running ls a 2nd time\n"; system( "ls" ); open STDOUT, '>&', $oldout; # restore the dup'ed filehandle to STDOUT warn "\n==STDOUT reopened:\n"; print "running ls a 3rd time\n"; system( "ls" );
(updated to add comments)

That sort of thing could make a big difference in the OP's situation, where there might be other stuff to be written back to a browser after running a system call.


In reply to Re^3: How to hide system(); output. by graff
in thread How to hide system(); output. by bcens

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.