Command Input (Backtick) Operator
First of all, we have the command input operator, also known as the backtick operator, because it looks like this:
$info = `perldoc $module`;
A string enclosed by backticks (grave accents, technically)
first undergoes variable interpolation just like a double-quoted
string. The result is then interpreted as a command line by the
system, and the output of that command becomes the value of the
pseudoliteral. (This is modelled after a similar
operator in Unix shells.) In scalar context, a single string
consisting of all the output is returned. In list context, a list of
values is returned, one for each line of output. (You can set
$/ to use a different line
terminator.)
The command is executed each time the pseudoliteral is
evaluated. The numeric status value of the command is saved in
$? (see Chapter 25 for the interpretation of $?, also known as $CHILD_ERROR).
Unlike the csh version of this command, no
translation is done on the return data—newlines remain newlines.
Unlike in any of the shells, single quotes in Perl do not hide
variable names in the command from interpretation. To pass a
$ through to the shell, you need
to hide it with a backslash. The $module in our perldoc example above is interpolated by
Perl, not by the shell. (Because the command undergoes shell
processing, see Chapter 20 for security
concerns.)
The generalized form of backticks is qx// (for “quoted execution”), but the operator works exactly the same way as ordinary backticks. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access