views:

1671

answers:

4

Heya,

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 paramter from one script to the next!

Many thanks in advance.

A: 

FYI, I'm not a perl guy. But in general, I think you would need to either set an environment variable and call the other script from the first one (so it could access the environment variable as a sub-process of the first one), or call the second script from the first and pass the argument on the command line.

Your ARGV variables are only going to be for the current running script, so you cannot use it like that to pass something from one to another.

crashmstr
Environment is passed from parent process to child process, so that won't work. The parent (shell) process will not see changes that the child (perl) has made to its environment.
ijw
Like I said, I'm not a perl guy, but I know you can do that in other languages and/or with OS functions.
crashmstr
Unless the code is running in the same process you will never see changes to the environment. This isn't a Perl thing, it is a POSIX thing.
Chas. Owens
Ok guys, I've updated my answer to remove what you are complaining about (which was a minor part of my answer anyway).
crashmstr
+5  A: 

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
R. Bemrose
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.
+1  A: 

@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;
Mykroft
+1  A: 

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("perl param_recv.pl");

OR

#!/usr/bin/perl
# param_send.pl
system("perl param_recv.pl 'VAL'");

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

muteW