Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, it will interpolate the variable. However, I highly recommend two things:</p> <ul> <li>Use <code>qx/../</code> instead of back ticks.</li> <li>Set the command you're running first in a Perl variable, and then interpolate that variable.</li> </ul> <p>The <code>qx/../</code> is nice because it makes it more obvious what you're doing:</p> <pre><code>my $variable = qx(ls $directory); </code></pre> <p>You could use a wide variety of characters for <code>qx</code>:</p> <pre><code>my $variable = qx(ls $directory); my $variable = qx/ls $directory/; my $variable = qx^ls $directory^; my $variable = qx#ls $directory#; </code></pre> <p>The character after the <code>qx</code> will be used as <em>quotes</em>. If you use parentheses, square brackets, or curly braces, you use them in pairs with the opening one and closing one.</p> <p>This means that you can avoid issues where a particular character in your command might confuse things.</p> <p>The other thing is to build your command into a Perl variable and then execute it when you try to do interpolations. This gives you more control over what you're doing:</p> <pre><code>my $HOME; my $command; $HOME = "bin"; $command = "ls $HOME"; print qx($command); #Lists the directory bin $command = 'ls $HOME'; print qx($command); #List user's home directory </code></pre> <p>In both of these examples, I'm doing <code>qx($command)</code>. However, in the first example, I allow Perl to substitute the value of <code>$HOME. In the second example, I use single quotes, so Perl doesn't substitute the value of</code>$HOME<code>. Instead, the string</code>$HOME` is just part of my command I'm using. I'm letting the shell interpolates it.</p> <p>I usualy am leery of any program that uses <code>qx/.../</code>. In most cases, it's used to run a command that could be done in Perl itself. For example, in early Perl programs, you'd see things like this:</p> <pre><code> $date = `date +%M/%Y/%D` chop $date; #Yeah, I said EARLY Perl programs </code></pre> <p>Because it was simply a lot easier to run the Unix command rather than trying to do it in a pure Perl way. However, doing it the Perl (i.e. <em>the correct</em>) way means you're no longer dependent upon the OS's behavior which is not entirely under your control.</p> <p>If you need the output of the command for use in your Perl script, you should use <a href="http://perldoc.perl.org/functions/open.html" rel="noreferrer">open</a> to execute your command, and treat the output of the command as a file.</p> <pre><code>my $command = "ls $HOME"; open my command_fh, "|-", $command or die qq(Couldn't execute "$command"); </code></pre> <p>If you simply need to execute the command, use the <a href="http://perldoc.perl.org/functions/system.html" rel="noreferrer">system</a> command:</p> <pre><code>my $command = "xyz $date"; my $error = system $command; if ( $error ) { #Something terrible happened... } </code></pre> <p>Note that if you send only a single scalar argument to the <code>system</code> command, <strong><em>and</em></strong> it contains possible shell meta characters, it will execute the command via the OS shell. If you send the <code>system</code> command a list to execute, <strong><em>or</em></strong> there are no shell metacharacters, Perl will call the command executor to execute the command directly without any shell interpolations:</p> <pre><code>my @command = qw(ls $HOME); system @command; #Will print out "No such directory '$HOME' </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload