Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Backtics and variables

by rpike (Scribe)
on Mar 16, 2010 at 13:40 UTC ( #828922=perlquestion: print w/ replies, xml ) Need Help??
rpike has asked for the wisdom of the Perl Monks concerning the following question:

When using backtics to execute a command will variables be substituted correctly if placed within? For example,
$result = 'somecommand $filename';
Will the variable filename have it value substituted in or will it get interpreted literally as $filename? If it doesn't is there something in Activestate's verion of Perl to allow me to accomplish this? Thanks.

Comment on Backtics and variables
Download Code
Re: Backtics and variables
by Corion (Pope) on Mar 16, 2010 at 13:43 UTC

    Single quotes don't interpolate. Double quotes and backticks do. See perlop. If you're curious about what a string contains, consider printing it.

Re: Backtics and variables
by jacaril (Beadle) on Mar 16, 2010 at 14:02 UTC
    Value will be subbed in with all Perl versions I have used.
    my $filename = "foo"; `echo $filename > $filename`;
    Creates file foo with 1 line foo in the file.
Re: Backtics and variables
by toolic (Chancellor) on Mar 16, 2010 at 14:22 UTC
    When using backtics to execute a command will variables be substituted correctly if placed within?
    Yes, backticks (qx) will interpolate variable values. As Corion said, refer to perlop.
    $result = 'somecommand $filename';
    Will the variable filename have it value substituted in or will it get interpreted literally as $filename?
    You used single quotes (q) in your example, not backticks. So, your question does not seem to match your example code. Since single quotes do not interpolate, $result will contain the literal string $filename, which you can easily prove to yourself by using print to inspect its value, as Corion also pointed out. You probably want:
    $result = `somecommand $filename`;
    or:
    $result = qx(somecommand $filename);

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://828922]
Approved by herveus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (11)
As of 2014-07-30 15:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (235 votes), past polls