The following prototypes demonstrate how one can capture the STDOUT
of a "worker" script, called by the "boss" script, as a FILEHANDLE
and turn the contents of FILEHANDLE
into an array that one can manipulate in the "boss" script. The boss script junk.pl
:
#!/usr/bin/perl
use strict; use warnings;
my $boppin;
my $worker = "junk2.pl";
open(FILEHANDLE, "$worker |");
my @scriptSTDOUT=<FILEHANDLE>;
close FILEHANDLE;
my $count=0;
foreach my $item ( @scriptSTDOUT ) {
$count++;
chomp $item;
print "$count\t$item\n";
}
if ( @scriptSTDOUT ) {
$boppin = pop @scriptSTDOUT; print "boppin=$boppin\n";
} else {
print STDERR "nothing returned by $worker\n";
}
my @array = ( '. . . \x09 wow\x21' , 5 );
system $worker, @array;
and the "worker" junk2.pl
:
#!/usr/bin/perl
use strict; use warnings; use 5.18.2;
print "$0";
This use of open
and definition of a FILEHANDLE
is okay if the $worker
does not require, as argument, an array whose elements contain whitespace. But if such a complex argument is required, one must call the "worker" script in list form, as at the bottom of the "boss" script. For "list form", see Calling a shell command with multiple arguments.
In that case, how does one capture the STDOUT
as a FILEHANDLE
? How can I modify the bottom line of the "boss" script to accomplish this?