Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^3: Invoke the Perl string interpolation engine on a string contained in a scalar variable.

by davido (Cardinal)
on Jan 15, 2019 at 20:32 UTC ( #1228618=note: print w/replies, xml ) Need Help??


in reply to Re^2: Invoke the Perl string interpolation engine on a string contained in a scalar variable.
in thread Invoke the Perl string interpolation engine on a string contained in a scalar variable.

In your example $y is a string of characters, but when you eval it with "eval $y", eval expects that string of characters to be Perl code. You need to give eval actual code to eval, not just a string of characters. Do this by wrapping that string of characters in Perl code (in quotes):

my $x = 'foo'; my $y = 'The answer is $x'; my $z = eval enquote($y); sub enquote { return 'qq{' . shift() . '}'; }

There's no great reason to have used the enquote sub rather than just inlining the buildup of the Perl-quoted string other than to attempt to make it more clear what the intent is. I could have just done this:

my $z = eval 'qq{' . $y . '}';

Or even...

my $z = eval "qq{$y}";

In any case, the goal is to turn a raw string of characters into something that eval can reasonably compile as code that when evaluated returns the original string with interpolation in place.


Dave

  • Comment on Re^3: Invoke the Perl string interpolation engine on a string contained in a scalar variable.
  • Select or Download Code

Replies are listed 'Best First'.
Re^4: Invoke the Perl string interpolation engine on a string contained in a scalar variable.
by ibm1620 (Monk) on Jan 16, 2019 at 01:33 UTC
    I get it. I never would have thought to nest it that much, but now I get it. Thanks.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2019-05-22 06:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (138 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!