Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Redirecting all output to a var for post processing?

by demian (Initiate)
on Feb 13, 2001 at 14:00 UTC ( #58114=categorized question: print w/replies, xml ) Need Help??
Contributed by demian on Feb 13, 2001 at 14:00 UTC
Q&A  > input and output


I'm trying to find a way to make a program run truely quiet,
ie no STDOUT/STDERR until the main routines finish. After
running, I want to analyze the STDOUT/STDERR filter it and
make choices based on the results.

The catch: I want to avoid creating any files in the process.

I've looked at the chpt 16 example of the cookbook, but
I don't quite get it??

Any clues?


Answer: Redirecting all output to a var for post processing?
contributed by goldclaw

If collecting all output in the same variable is OK for you, then using Tie::STDERR would do the job:

use vars qw($OUTPUT); use Tie::STDERR \$OUTPUT; *STDOUT=\*STDERR;
You might want to save the value of STDOUT if you would like to print something after you are done filtering etc.


Answer: Redirecting all output to a var for post processing?
contributed by zigster

Sorry I dont quite follow, if you want to make your program run quietly then just dont print anything to stdout??!! Could you not just concat any messages to an internal variable instead of printing. If you really want to

Do you mean that you want to capture stdout/stderr of a program that you fork? If that is the case then you could connect the child process to the parent via pipes and capture the op directly from the pipes (see below).

If you really want to capture stdout in this way you could wrap the program in a fork as in this incomplete example.

if ($pid = open(CHILD,"-|")) { while (<CHILD>) { $data .= $_; } print $data; } else { exec ("ls"); }
If you want to keep all the code in one module you could replace the exec with your parent code.

Please (register and) log in if you wish to add an answer

  • 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, 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
  • 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.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (3)
    As of 2020-09-30 01:08 GMT
    Find Nodes?
      Voting Booth?
      If at first I donít succeed, I Ö

      Results (156 votes). Check out past polls.