Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
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 (Bishop) 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 pondering the Monastery: (9)
As of 2015-07-07 10:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (88 votes), past polls