It also won't handle
$string = '\\$var';     # escaped '$'
and
$string = '${foo}bar';  # $foo, not $foobar
which serve necessary functions (unlike
$string = '$_->[4]';
which is just a feature).

As a bonus, I've added support for \n and \t. It's easy to add the more escapes.

The following code does, and like yours, limits the strings being eval'ed to a minumum.

our %ESCAPES = ( n => "\n", t => "\t", ); my $interpolated = ''; { local *_ = \$string; # Alias $_ to $string. for (;;) { if (/\G \$(\w+) /gcsx || /\G \${(\w+)} /gcsx) { $interpolated .= eval '$'.$1; next; } if (/\G \\(.) /gcsx) { $interpolated .= exists($ESCAPES{$1}) ? $ESCAPES{$1} : $1; next; } /\G ( . # Catchall. (?: # These four lines are optional. (?!\\) # They are here to speed things up (?!\$) # by avoiding adding individual .)* # characters to the $interpolated. ) /gcsx && do { $interpolated .= $1; next; }; last; } }

You can even get rid of eval completely if you don't interpolate from lexicals:

our %ESCAPES = ( n => "\n", t => "\t", ); sub interpolate { local *_ = \$_[0]; # Alias $_ to $_[0]. my $interpolated = ''; for (;;) { if (/\G \$(\w+) /gcsx || /\G \${(\w+)} /gcsx) { no strict 'refs'; #no warnings; $interpolated .= ${$1}; next; } if (/\G \\(.) /gcsx) { $interpolated .= exists($ESCAPES{$1}) ? $ESCAPES{$1} : $1; next; } /\G ( . # Catchall. (?: # These four lines are optional. (?!\\) # They are here to speed things up (?!\$) # by avoiding adding individual .)* # characters to the $interpolated. ) /gcsx && do { $interpolated .= $1; next; }; last; } return $interpolated; }

All told, though, it's safer and cleaner to only interpolate from variables in hash:

our %ESCAPES = ( n => "\n", t => "\t", ); sub interpolate { local *_ = \$_[0]; # Alias $_ to $_[0]. my $symtab = $_[1]; my $interpolated = ''; for (;;) { if (/\G \$(\w+) /gcsx || /\G \${(\w+)} /gcsx) { if (!exists($symtab->{$1})) { $interpolated .= "[unknown symbol \$$1]"; } elsif (!defined($symtab->{$1})) { $interpolated .= "[undefined symbol \$$1]"; } else { $interpolated .= $symtab->{$1}; } next; } if (/\G \\(.) /gcsx) { $interpolated .= exists($ESCAPES{$1}) ? $ESCAPES{$1} : $1; next; } /\G ( . # Catchall. (?: # These four lines are optional. (?!\\) # They are here to speed things up (?!\$) # by avoiding adding individual .)* # characters to the $interpolated. ) /gcsx && do { $interpolated .= $1; next; }; last; } return $interpolated; } my %symtab = ( string => "cheese", #user => $user, #... ); my $need_to_interpolate = 'smell my $string\n'; print interpolate($need_to_interpolate, \%symtab);

In reply to Re^2: Variable Interpolation by ikegami
in thread Variable Interpolation by reasonablekeith

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.