I want to have any data that would go to STDOUT, STDERR, STDIN to go to a variable. (Ideally just one variable by duping the file handles, but will settle for something close to what I have here.) What am I doing wrong here? I was expecting to get the error from the bad "system" call.
#!/usr/bin/perl use strict; use warnings; close(STDOUT); close(STDIN); close(STDERR); my $strout; my $strin; my $strerr; open(STDOUT, "+<", \$strout) or die; open(STDIN, "+<", \$strin) or die; open(STDERR, "+<", \$strerr) or die; print STDOUT "Hello this is 1 stdout\n"; print STDERR "Hello this is 1 stderr\n"; print STDIN "Hello this is 1 stdin\n"; system("fin . -name asasdasdasd"); print STDOUT "Hello this is 2 stdout\n"; print STDERR "Hello this is 2 stderr\n"; print STDIN "Hello this is 2 stdin\n"; close(STDOUT); close(STDIN); close(STDERR); open(LOG, ">log") or die; print LOG $strout, $strin, $strerr; close(LOG);
cat log Hello this is 1 stdout Hello this is 2 stdout Hello this is 1 stdin Hello this is 2 stdin Hello this is 1 stderr Hello this is 2 stderr

In reply to printing to a scalar -- STDIN, STDOUT, STDERR by Jack B. Nymbol

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.