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

I'm needing to write something similar to prove which summarizes a number of executed tests.

I have a number of perl scripts which output to STDOUT which I would like to capture in total (but still output to STDOUT...). The three alternatives I have thought of are:

Thanks for any candor which you may care to share.

Replies are listed 'Best First'.
Re: equivalent to prove?
by samtregar (Abbot) on Jan 29, 2009 at 22:15 UTC
    It's easy to capture STDOUT and then send it along. When you launch your sub-script do it like this:

       open(SUB, "sub.pl |") or die $!

    The "|" at the end tells Perl to do a fork()/exec() and hook up STDOUT from sub.pl to your SUB filehandle. Then you can do:

    while (my $line = <SUB>) { # do something with $line... # then print it out: print $line; }

    Give it a try and post again if you run into trouble.

    -sam

Re: equivalent to prove?
by Marshall (Canon) on Jan 30, 2009 at 11:53 UTC
    There is a standard program called "tee" that does this. There all sorts of versions of this that you can download in a directly executable format. Below is a simple version written in Perl. Typical output buffer is about 4K bytes. You don't need to turn output buffering off if your program is generating lots of output - buffer gets flushed when it is full. There are various options on standard "tee" to append, overwrite, etc. This is just an example of what it does.

    basic sytanx: $someprog | tee somefile

    #file: tee.pl use strict; use warnings; $| = 1; # **** turn off output buffering ***** sub usage () { print "TEE USAGE:\n tees stdout to a file and to stdout\n". " program | tee outfile\n". " sends stdout from program to outfile\n"; exit; } my $filename = shift @ARGV; usage unless $filename; open (OUTFILE, ">>$filename") || die "Can't open OUTFILE: $!"; while (<>){ print; print OUTFILE; }
      I may not have gotten exactly the gist of what you wanted here, but cat tee.pl | tee junk >anotherprog would run cat on tee.pl, save stdout in junk and pass it on to anotherprog. In this case, nothing gets printed to the screen (unless you tee again, which is also possible!).

        Nope, that tells it to write the output to 'anotherprog'. To do what you described, you'd want:

        cat tee.pl | tee junk | anotherprog