http://www.perlmonks.org?node_id=399033


in reply to Interpolating variables in a string

You could try doing a here-script type of thing:
my $token = "__UNIQUE_TOKEN__"; $token .= "_" while $str =~ /$token/; chomp (my $new_str = eval qq(<<"$token"\n$str\n$token));
(I haven't tested that, but it feels like it might work). What I usually do for this type of thing is to not rely on lexical variables in my program, and instead set up a separate hash of legal variables:
my %vars = ( var => "foo" ); $str = 'abc $var def'; (my $new_str = $str) =~ /\$(\w+)/$vars{$1}/g;
--Dave
Opinions my own; statements of fact may be in error.