in reply to Invoke the Perl string interpolation engine on a string contained in a scalar variable.

If you trust your input data (and I mean, really trust it), there is eval.

Otherwise, whitelisting the variables is the sanest approach, and then basically doing s/\$(\w+)/$1/gee.

Personally, I wouldn't allow a template to access arbitrary variables in the script and always decouple the template variable names from the inner workings of the program by using a hash, just like String::Interpolate does.

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

Replies are listed 'Best First'.
Re^2: Invoke the Perl string interpolation engine on a string contained in a scalar variable.
by ibm1620 (Hermit) on Jan 15, 2019 at 20:13 UTC
    Since it's just for my own set of tools which I share with a few other colleagues, I trust it.

    But I've tried eval several ways and can't get what I want:

    : my $x = 'foo'; my $y = 'The answer is $x'; my $z; eval {$z=$y}; say $z; # or say eval $y; > The answer is $x
    $x is still uninterpolated.

      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

        I get it. I never would have thought to nest it that much, but now I get it. Thanks.

      You need to eval a string:

      my($greeting,$target)=('Hello','World'); my $str = '$greeting $target'; $res=eval "qq{$str}"; print $res __END__ Hello World