The above responses correctly suggest the
/e modifier... though as i interperet OP's needs, "KEYWORD" needs to be used to get the value (i'll assume via a hash, but could be a function call or something else) ...
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
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.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.