When using system() calls in Perl, do you have to escape the shell args, or is that done automatically?
The arguments will be user input, so I want to make sure this
If you use system $cmd, @args
rather than system "$cmd @args"
(an array rather than a string), then you do not have to escape the arguments because no shell is invoked (see system). system {$cmd} $cmd, @args
will not invoke a shell either even if $cmd contains metacharacters and @args is empty (this is documented as part of exec). If the args are coming from user input (or other untrusted source), you will still want to untaint them. See -T
in the perlrun docs, and the perlsec docs.
If you need to read the output or send input to the command, qx
and readpipe
have no equivalent. Instead, use open my $output, "-|", $cmd, @args
or open my $input, "|-", $cmd, @args
although this is not portable as it requires a real fork
which means Unix only... I think. Maybe it'll work on Windows with its simulated fork. A better option is something like IPC::Run, which will also handle the case of piping commands to other commands, which neither the multi-arg form of system nor the 4 arg form of open will handle.