Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass arguments to system command in perl

Tags:

perl

My command:

system("start cmd.exe /k C:\\script.pl $arg1 $arg2 $arg3");

is not passing the arguments correctly. What is the correct way to do this?

Thx

like image 631
user0 Avatar asked Dec 26 '22 20:12

user0


1 Answers

The best way to invoke system is with an array or a list:

my @args = ("start", "cmd.exe", "/k", "C:\\script.pl", $arg1, $arg2, $arg3);
system @args;

system "start", "cmd.exe", "/k", "C:\\script.pl", $arg1, $arg2, $arg3;

Compared with a single string to system, this saves the complexities of 'how to quote the arguments' because the shell doesn't get a chance to interpret them. On the other hand, if you want the shell to do I/O redirection or piping, you probably won't use this mechanism.

like image 53
Jonathan Leffler Avatar answered Jan 12 '23 00:01

Jonathan Leffler