2

I have a script which I run and after it's run it has some information that I need to pass to the next script to run.

The Unix/DOS commands are like so:

perl -x -s param_send.pl
perl -x -s param_receive.pl

param_send.pl is:

# Send param

my $send_var = "This is a variable in param_send.pl...\n";
$ARGV[0] = $send_var;
print "Argument: $ARGV[0]\n";

param_receive.pl is:

# Receive param

my $receive_var = $ARGV[0];
print "Parameter received: $receive_var";

But nothing is printed. I know I am doing it wrong but from the tutorials I can't figure out how to pass a parameter from one script to the next!

brian d foy
  • 129,424
  • 31
  • 207
  • 592

4 Answers4

9

You can use a pipe character on the command line to connect stdout from the first program to stdin on the second program, which you can then write to (using print) or read from (using the <> operator).

perl param_send.pl | perl param_receive.pl

If you want the output of the first command to be the arguments to the second command, you can use xargs:

perl param_send.pl | xargs perl param_receive.pl
brian d foy
  • 129,424
  • 31
  • 207
  • 592
Powerlord
  • 87,612
  • 17
  • 125
  • 175
  • This solution seems to do exactly what I need. Thanks! I can freely pass parameters from the param_send script to the param_receive script. Thanks again. –  May 27 '09 at 15:29
2

The %ENV hash in Perl holds the environment variables such as PATH, USER, etc. Any modifications to these variables is reflected 'only' in the current process and any child process that it may spawn. The parent process (which happens to be the shell in this particular instance) does not reflect these changes so when the 'param_send.pl' script ends all changes are lost.

For e.g. if you were to do something like,

#!/usr/bin/perl
# param_send.pl
$ENV{'VAL'} = "Value to send to param_recv";


#!/usr/bin/perl
# param_recv.pl
print $ENV{'VAL'};

This wouldn't work since VAL is lost when param_send exits. One workaround is to call param_recv.pl from param_send.pl and pass the value as an environment variable or an argument,

#!/usr/bin/perl
# param_send.pl
$ENV{'VAL'} = "Value to send to param_recv";
system( $^X, "param_recv.pl");

OR

#!/usr/bin/perl
# param_send.pl
system( $^X, qw(param_recv.pl 'VAL') );

Other options include piping the output or you could check out this Perlmonks node for a more esoteric solution.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
aks
  • 24,359
  • 3
  • 32
  • 35
1

@ARGV is created at runtime and does not persist. So your second script will not be able to see the $ARGV[0] you assigned in the first script. As crashmstr points out you either need to execute the second script from the first using one of the many methods for doing so. For example:

my $send_var = "This is a variable in param_send.pl...\n";
`perl param_receive.pl $send_var`;

or use an environment variable using %ENV:

my $send_var = "This is a variable in param_send.pl...\n";
$ENV['send_var'] = $send_var;
Community
  • 1
  • 1
Mykroft
  • 13,077
  • 13
  • 44
  • 72
-1

For a more advanced solutions think about using sockets or IPC.

OneOfOne
  • 95,033
  • 20
  • 184
  • 185