in reply to Interpolating variables in a string
(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 $token = "__UNIQUE_TOKEN__"; $token .= "_" while $str =~ /$token/; chomp (my $new_str = eval qq(<<"$token"\n$str\n$token));
my %vars = ( var => "foo" ); $str = 'abc $var def'; (my $new_str = $str) =~ /\$(\w+)/$vars{$1}/g;
|
|---|