Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Redirecting all output to a var for post processing?

by demian (Initiate)
on Feb 13, 2001 at 14:00 UTC ( [id://58114]=perlquestion: print w/replies, xml ) Need Help??

demian has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to find a way to make a program run truly quiet, i.e. 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 example from chapter 16 of The Perl Cookbook, but I don't quite get it.

Originally posted as a Categorized Question.

  • Comment on Redirecting all output to a var for post processing?

Replies are listed 'Best First'.
Re: Redirecting all output to a var for post processing?
by goldclaw (Scribe) on Feb 14, 2001 at 01:06 UTC
    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.

    goldclaw

Re: Redirecting all output to a var for post processing?
by zigster (Hermit) on Feb 13, 2001 at 15:27 UTC

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://58114]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-19 12:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found