in reply to Evaluating a regex replacement value
one last note to OP: if your intent was like above, but instead of a hash you have variables named $foo, $bar, and $stuff and those are what you want used, then it is possible to do eval('$'.$1), but not recommended -- it is probably better to rework the code to use a hash or function call to look of the value instead.use strict; use warnings; my %values = ( foo => 3, bar => 4, stuff => 5 ); while( <DATA> ){ s/<:(KEYWORD):(?:([^:>]+:)?>/ $values{$1} * ($2 || 1) /e; print; } __DATA__ <:foo:4:> <:bar:5:> <:bar:> <:stuff:9:> # OUTPUT: 12 20 4 45
|
|---|