使用参数在 perl 脚本中执行 perl 脚本

LKT*_*LKT 1 perl arguments

当我尝试在我的 perl 脚本中执行 perl 脚本时遇到了一个问题。这是我正在进行的一个更大项目的一小部分。

下面是我的 perl 脚本代码:

use strict;
use warnings;
use FindBin qw($Bin);

#There are more options, but I just have one here for short example
print "Please enter template file name: "
my $template = <>;
chomp($template);

#Call another perl script which take in arguments
system($^X, "$Bin/GetResults.pl", "-templatefile $template");
Run Code Online (Sandbox Code Playgroud)

“GetResults.pl”接受多个参数,例如我在这里只提供一个。基本上,如果我单独使用 GetResults.pl 脚本,我会在命令行中输入:

perl GetResults.pl -templatefile template.xml
Run Code Online (Sandbox Code Playgroud)

我在上面的系统函数调用中遇到了两个问题。首先,当我运行我的 perl 脚本导致 GetResults.pl 中的无效参数错误时,它似乎删除了我的参数前面的破折号。

然后我尝试了这个

system($^X, "$Bin/GetResults.pl", "/\-/templatefile $template");
Run Code Online (Sandbox Code Playgroud)

看起来没问题,因为它没有抱怨早期的问题,但现在它说它找不到 template.xml,尽管我将该文件与我的 perl 脚本以及 GetResults.pl 脚本放在同一位置。如果我只是单独运行 GetResults.pl 脚本,它工作正常。

我想知道当我使用变量 $template 和位于我的 PC 上的真实文件名(我使用的是 Window 7)时,字符串比较是否存在一些问题。

我是 Perl 的新手,希望有人能提供帮助。先感谢您。

Jon*_*ler 5

将参数作为数组传递,就像您使用任何其他程序一样(Perl 脚本并不特殊;它是 Perl 脚本是一个实现细节):

system($^X, "$Bin/GetResults.pl", "-templatefile", "$template");
Run Code Online (Sandbox Code Playgroud)

您可以将所有内容排列在一个数组中并使用它:

my @args = ("$Bin/GetResults.pl", "-templatefile", "$template");
system($^X, @args);
Run Code Online (Sandbox Code Playgroud)

甚至添加$^X@args. 等等。