Reputation: 3391
I have on a windows machine (with perl interpreter on environmental path) the command :
perl script1.pl | perl script2.pl
and the contents of script one are just:
print "AAA";
and the contents of script 2 is just:
$r = shift;
print $r;
And the command doesn't pipe correctly. How do you do this? Also, if you were to do it with a Filehandle, how would you run a perl script from another script concurrently. The following doesn't work:
open F, '| perl script2.pl AAA';
Upvotes: 4
Views: 2478
Reputation: 139531
Remember that shift
in your main program removes elements from the special array @ARGV
that holds the current invocation’s command-line arguments.
Write script2 as a filter
#! perl
while (<>) {
print "$0: ", $_;
}
or even
#! perl -p
s/^/$0: /;
To set up the pipeline from script1, use
#! perl
use strict;
use warnings;
open my $fh, "|-", "perl", "script2.pl"
or die "$0: could not start script2: $!";
print $fh $_, "\n" for qw/ foo bar baz quux /;
close $fh or die $! ? "$0: close: $!"
: "$0: script2 exited $?";
Multi-argument open
bypasses the shell’s argument parsing—an excellent idea on Windows. The code above assumes both scripts are in the current directory. Output:
script2.pl: foo script2.pl: bar script2.pl: baz script2.pl: quux
Upvotes: 1
Reputation: 3037
You should be reading from STDIN, shift manipulates command-line arguments.
The following snippet explains it.
cat script1.pl
print "AAA";
cat script2.pl
print <STDIN>;
Upvotes: 1