I want to invoke a shell command from a Perl script. The arguments to the command are present in a Perl array.
What's the simplest way to do this?
Thanks for replies
You probably need to call system, and it is most efficient to pass the parameters as a list, which avoids using the shell to parse the command line. A call like
my $status = system 'command', @arguments;
should do what you need.
There are usually two forms accepted by most functions to launch programs:
The first is safer, and require fewer resources.
system($prog, @args); # @args > 0
system({ $prog } $prog, @args); # @args >= 0
But if you must use the shell or if you must provide a shell command, there are String::ShellQuote (if you're on a unix system) or Win32::ShellQuote (if on a Win32 system) to help you.
use String::ShellQuote qw( shell_quote );
my $shell_cmd = shell_quote($prog, @args);
system($shell_cmd);