如何将参数从一个Perl脚本传递到另一个?

时间:2021-11-22 06:59:17

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:

Unix / DOS命令是这样的:

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.

提前谢谢了。

5 个解决方案

#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).

您可以在命令行上使用管道字符将stdout从第一个程序连接到第二个程序的stdin,然后您可以将其写入(使用print)或读取(使用<>运算符)。

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:

如果希望第一个命令的输出是第二个命令的参数,则可以使用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:

@ARGV是在运行时创建的,不会持久存在。因此,您的第二个脚本将无法看到您在第一个脚本中分配的$ ARGV [0]。正如crashmstr指出的那样,您需要使用众多方法中的一种来执行第一个脚本。例如:

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:

或使用%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.

Perl中的%ENV哈希包含环境变量,例如PATH,USER等。对这些变量的任何修改都只在当前进程中反映,并且可以生成任何子进程。父进程(恰好是此特定实例中的shell)不反映这些更改,因此当'param_send.pl'脚本结束时,所有更改都将丢失。

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,

这不起作用,因为当param_send退出时VAL会丢失。一种解决方法是从param_send.pl调用param_recv.pl并将该值作为环境变量或参数传递,

#!/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.

其他选项包括管道输出或您可以检查此PM节点以获得更深奥的解决方案。

#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.

仅供我,我不是一个perl家伙。但总的来说,我认为您需要设置一个环境变量并从第一个脚本调用另一个脚本(因此它可以作为第一个脚本的子流程访问环境变量),或者调用第二个脚本。首先在命令行上传递参数。

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.

您的ARGV变量仅适用于当前运行的脚本,因此您不能像这样使用它来将某些内容从一个传递到另一个。

#5


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

对于更高级的解决方案,请考虑使用套接字或IPC。

#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).

您可以在命令行上使用管道字符将stdout从第一个程序连接到第二个程序的stdin,然后您可以将其写入(使用print)或读取(使用<>运算符)。

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:

如果希望第一个命令的输出是第二个命令的参数,则可以使用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:

@ARGV是在运行时创建的,不会持久存在。因此,您的第二个脚本将无法看到您在第一个脚本中分配的$ ARGV [0]。正如crashmstr指出的那样,您需要使用众多方法中的一种来执行第一个脚本。例如:

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:

或使用%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.

Perl中的%ENV哈希包含环境变量,例如PATH,USER等。对这些变量的任何修改都只在当前进程中反映,并且可以生成任何子进程。父进程(恰好是此特定实例中的shell)不反映这些更改,因此当'param_send.pl'脚本结束时,所有更改都将丢失。

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,

这不起作用,因为当param_send退出时VAL会丢失。一种解决方法是从param_send.pl调用param_recv.pl并将该值作为环境变量或参数传递,

#!/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.

其他选项包括管道输出或您可以检查此PM节点以获得更深奥的解决方案。

#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.

仅供我,我不是一个perl家伙。但总的来说,我认为您需要设置一个环境变量并从第一个脚本调用另一个脚本(因此它可以作为第一个脚本的子流程访问环境变量),或者调用第二个脚本。首先在命令行上传递参数。

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.

您的ARGV变量仅适用于当前运行的脚本,因此您不能像这样使用它来将某些内容从一个传递到另一个。

#5


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

对于更高级的解决方案,请考虑使用套接字或IPC。