How can I pass arguments from one Perl script to another?

十年热恋 提交于 2020-01-02 10:27:46

问题


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.


回答1:


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



回答2:


@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;



回答3:


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.




回答4:


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.




回答5:


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



来源:https://stackoverflow.com/questions/915937/how-can-i-pass-arguments-from-one-perl-script-to-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!