4

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

0

1 Answer 1

10

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.

Sign up to request clarification or add additional context in comments.

4 Comments

You don't need the array, you can just supply a list.
Agreed; you don't formally need an array variable. See update.
using the list form of system will prevent perl from launching a shell in order to run start, but in this case start is then just running a cmd.exe shell anyway and most of the benefit is lost. Perhaps Win32::Console could also be used to eliminate the start.
@tjd: Now you're delving into territory outside my scope — I know nothing of the Win32 packages beyond 'they exist' and 'people seriously using Windows might need to know about them'. I currently don't use Windows at all. That means you're probably right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.