4

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';
Sam Adamsh
  • 3,331
  • 8
  • 32
  • 53
  • [Related](https://stackoverflow.com/questions/2480069/how-can-i-read-piped-input-in-perl-on-windows) – ggorlen Oct 01 '20 at 20:09

2 Answers2

1

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>;
tuxuday
  • 2,977
  • 17
  • 18
1

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
Greg Bacon
  • 134,834
  • 32
  • 188
  • 245