Beefy Boxes and Bandwidth Generously Provided by pair Networks chromatic writing perl on a camel
Problems? Is your data what you think it is?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

I am attempting to redirect STDERR to a scalar. However, I'm using ActiveState Perl v5.6.1 (MSWin32-x86-multi-thread ) build 635. The only way that I found to work (so far anyway) is as follows:

  1. Save a copy of STDERR.
  2. Redirect it to a temporary file.
  3. Run the process that only outputs on STDERR.
  4. Restore the original STDERR.
  5. Slurp the contents of the temporary file into a scalar.

I have stripped down my code greatly to produce the following example script. Both &oldway and &newway work fine but which one is the best way of doing this?

use File::Temp 'tempfile'; print "Trying oldway...\n"; my $oldway = oldway(); # should not display anything print "Finished with oldway.\n"; print $oldway; print "Testing STDERR...\n"; print STDERR "(STDERR) can you hear me?\n"; print "Trying newway...\n"; my $newway = newway(); # should not display anything print "Finished with newway.\n"; print $newway; print "Testing STDERR...\n"; print STDERR "(STDERR) can you hear me now?\n"; sub oldway { my ($fh, $filename) = tempfile(UNLINK => 1); open(OLDERR, ">&STDERR") || die "Can't save STDERR, $!"; # save STDE +RR open(STDERR, '>', $filename) || die "Can't redirect STDERR to the te +mp file, $!"; select(STDERR); $| = 1; print STDERR "oldway says 'hi'\n"; open(STDERR, ">&OLDERR"); # restore the original STDERR select(STDOUT); my $oldway = slurp_file($filename); return $oldway; } sub newway { my ($fh, $filename) = tempfile(UNLINK => 1); *oldfh = select STDERR; # save STDERR open(STDERR, '>', $filename) || die "Can't redirect STDERR to the te +mp file, $!"; select(STDERR); $| = 1; print STDERR "newway says 'hi'\n"; open(STDERR, ">&oldfh"); # restore STDERR select(STDOUT); my $newway = slurp_file($filename); return $newway; } sub slurp_file { my $filename = shift; my $slurp; open(TXT, '<', $filename) || die "Can't read the file: $filename, $! +"; { local $/ = undef; $slurp = <TXT>; # slurp! } close(TXT); return $slurp; } __DATA__ Trying oldway... Finished with oldway. oldway says 'hi' Testing STDERR... (STDERR) can you hear me? Trying newway... Finished with newway. newway says 'hi' Testing STDERR... (STDERR) can you hear me now?


In reply to Best Way to Redirect STDERR to a Scalar by Mr. Muskrat

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (8)
    As of 2014-04-23 23:54 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (557 votes), past polls