Perl: combine Backtick & system() I/O operation?

Hi - Within perl I want to execute a system command. I want to re-direct all the output from the command to a file (@result = `$cmd`;), but I ALSO want the results to be displayed on the screen (system("$cmd");

The reason is this - if the command completes, I want to process the output. If the command -hangs- (trying to debug a system problem), I want the output on the screen so I can see how far the command got.

Any easy way to do this in perl?

Thanks!
/j

You may want to check out the "Tee" module - Perl's implementation of the "tee" command.

$ 
$ # let's assume you want to run the command "seq 1 10" from within Perl
$ perl -le 'use Tee; tee("seq 1 10", "testfile.out")'
1
2
3
4
5
6
7
8
9
10
$ 
$ cat testfile.out
1
2
3
4
5
6
7
8
9
10
$ 

tyler_durden

well, first, apparently ActivePerl doesn't support Tee. But if it did, are you saying this would work?

#!/usr/bin/perl -w

use Tee;

tee("dir",@results); # puts output of "dir" results to screen, and into @results

foreach $line (@results) {  # now print the results again just to show they were captured in the array
    print $line;
}

thanks
/j

No, that second argument is either a filename or an array of filenames to which the output of command ("dir") would be redirected.

$ # print the output of "dir" on the screen
$ # and also redirect the output of "dir" to the file "file1.out"
$ perl -le 'use Tee; tee("dir", "file1.out")'
testfile  testfile.out
$ 
$ # check the content of the file "file1.out" now
$ cat file1.out
testfile  testfile.out
$ 
$ # the same as what was printed on the screen

Same thing with an array of filenames:

$ 
$ # print the output of "dir" on the screen
$ # and also redirect the output of "dir" to the files in the array
$ # "@files" i.e. file2.out, file3.out and file4.out
$ perl -le 'use Tee; @files = qw (file2.out file3.out file4.out); tee("dir", @files)'
file1.out  testfile  testfile.out
$ 
$ # now check the contents of file2.out, file3.out and file4.out
$ cat file2.out
file1.out  testfile  testfile.out
$ 
$ cat file3.out
file1.out  testfile  testfile.out
$ 
$ cat file4.out
file1.out  testfile  testfile.out
$ 
$ 

tyler_durden

Ummm - well thanks then. Although a useful answer, it doesn't match my question :).

Is there any way to direct the output of a system cmd to an array and the screen simultaneously?

Maybe not out-of-the-box, but it certainly can be installed from CPAN, check the ppm command

Nope. Check the Tee CPAN page for syntax: Tee @ CPAN

---------- Post updated at 21:48 ---------- Previous update was at 21:21 ----------

Try this:

#!/usr/bin/perl

use strict;
use warnings;

my @arr;

print "Output from screen:\n";

open(FROM, "ls |") or die "Could not open pipe: $!\n";

while (<FROM>) {
chomp;
print "$_\n";
push @arr, $_;
}

close(FROM);

print "Output from array:\n";

 foreach (@arr) {
 chomp;
 print "$_\n";
 }

pseudocoder - that looks like it would work, but I wonder when the command is actually executed. If it's executed during the OPEN and then hangs, I don't think it will ever get to the first print statement?

thanks though

I will look into downloading tee - adding packages is new to me though
/j

Update - you know - I think what I'm looking for doesn't exist
tee($command,@array) where the output of the command goes to STDOUT and into the @array. all the Tee that I can find at CPAN writes the output to -files-. I suppose I could create a temp file and then read it back in, but that's sort of inelegant?

am I missing something here?

thanks!
/j