Question

I wrote a Perl script that launches another program and I want it to pass the exact ARGV parameters it received to the launched program.

Currently I use this code:

my $cmd = "script.sh"
my $params_str = "";
foreach my $param (@ARGV) {
        $params_str .= " $param";
}

$cmd .= " " . $params_str;
system($cmd);

But I am afraid that the following scenario will be problematic:

>perl script.pl "param with spaces" param2 param3

As it will call my $cmd with the first parameter separated into 3 parameters, as the quotation marks would be removed: param with spaces param2 param3

Is there an elegant way to pass the parameters correctly?

Was it helpful?

Solution

system() can be used with list of parameters,

system("script.sh", @ARGV);

which is also preferable way of system usage, and you don't have to worry about "param with spaces".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top