Jan 202013
Article Perl

As is always the case in Perl, there is more than one way to get things done. In this post we review some of the possible ways to launch the execution of an external command from a perl script.

Ways to execute an external command

The most common ways are:

  • the system function
  • the exec function
  • the “backtick” operator ()
  • theopen function

All these methods trigger the execution of an external command, but each of them does it in a slightly different way. In summary:

The system() function executes the command passed as argument, waits until it finishes and returns the return code, but does not return the output that the command can write to STDOUT.

The call to the exec() function completely replaces the running script with the execution of the command passed as argument.

The “backticks” operator return the output written by the command to STDOUT.

The open() function, normally used to open a file, can be used to open an input or output pipe between the script and the command being executed.

Below is a more detailed explanation of each of these possibilities.

The system() function

The system() function runs the command, and returns the return code in the $? variable.

Return code

The return code is similar to the code returned by a call to “wait”. Therefore, to get the actual return code from the program, this value has to be right-shifted 8 bits ($? >> 8).

The value -1 means that the command could not be executed. In this case, the variable $! holds a message with the reason of the failure. For instance:

Argument passing

The system() function takes as input an scalar or an array. If the argument is scalar, the command is executed inside a shell (equivalent to running the command as “/bin/sh -c command”); if the argument is an array, it is executed directly, taking the first element of the array as the command name, and the other elements as arguments to be passed to the command.

For efficiency and security, it is preferrable to pass the argument as an array (mainly if the perl script is a CGI).


#– run “command” with three arguments, passing it to system() as a single text string
system(“command arg1 arg2 arg3”);

#– run “command”, passing the command and its arguments in an array
system(“command”, “arg1”, “arg2”, “arg3”);

The exec() function

As we have already mentioned, the exec() function replaces the running script with the command being executed. There is only an exception to this rule if the command does not exist, AND the exec() function has been called with an array argument.

The “backticks” () operator

This operator is used by enclosing the command in “`” characters.

The output from the command is returned to the script, and the return value is saved in the $? variable.

If the output from the command is more than one line, and it is assigned to a scalar variable, the value is returned as a single text string. If the value is assigned to an array, each line is returned as an element of the array:


Only the output written to STDOUT is returned. It is possible however to redirect to STDOUT the output written to STDERR, in case we need to capture it:

The open() function

Calling open(), it is possible:

– To capture the output from a command, using the syntax open(“command |”)

– To start a command, and write data from the perl script into the STDIN of the command, using the syntax open(“| command”)


Index of posts related to Perl programming

 Posted by at 5:08 pm

 Leave a Reply