Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Backtics and variables

by rpike (Scribe)
on Mar 16, 2010 at 13:40 UTC ( [id://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.

Replies are listed 'Best First'.
Re: Backtics and variables
by Corion (Patriarch) 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 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);
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-03-29 08:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found