Why doesn"t it evaluate in RE using "e" modifier ??

Why doesn"t it evaluate in RE using "e" modifier ??

am 02.01.2008 11:50:30 von ahmad

Using the following example:

$ab=7;
$str='$ab';
$str=~s/(\$\w+)/3+$1/eeeg ;
print $str;

It returns 3 (I expect 10!!!) Where's the mistake?
thanks and regards,
Ahmad

Re: Why doesn"t it evaluate in RE using "e" modifier ??

am 02.01.2008 13:10:36 von Christian Winter

Ahmad wrote:
> Using the following example:
>
> $ab=7;
> $str='$ab';
> $str=~s/(\$\w+)/3+$1/eeeg ;
> print $str;
>
> It returns 3 (I expect 10!!!) Where's the mistake?

The first evaluation of the replacement pattern already tries
to execute the addition, but at this point the second operator
of the addition is still the string '$ab', which evaluates to
zero in numeric context. To have it behave correctly, you need
to make sure that the expression returns the final term only in
the last step (btw., three "e" modifiers are overkill here):

$str=~s/(\$\w+)/"3 + " . $1/eeg;

HTH
-Chris

Re: Why doesn"t it evaluate in RE using "e" modifier ??

am 02.01.2008 13:10:51 von rvtol+news

Ahmad schreef:
> Using the following example:
>
> $ab=7;
> $str='$ab';
> $str=~s/(\$\w+)/3+$1/eeeg ;
> print $str;
>
> It returns 3 (I expect 10!!!) Where's the mistake?

See http://template-toolkit.org/

--
Affijn, Ruud

"Gewoon is een tijger."

Re: Why doesn"t it evaluate in RE using "e" modifier ??

am 02.01.2008 16:30:33 von Paul Lalli

On Jan 2, 5:50=A0am, Ahmad wrote:
> Using the following example:
>
> $ab=3D7;
> $str=3D'$ab';
> ;
> print $str;
>
> It returns 3 (I expect 10!!!) Where's the mistake?

Each /e is equivalent to an eval(). So you have:

eval(eval(eval('3 + $1')));
doing the first eval, that comes out to:
eval(eval(3 + '$ab'))
3 + '$ab' of course, is 3, since '$ab' is a string being used in
numeric context
so now you have
eval(eval(3))
which of course is
eval(3)
which of course is
3

Instead, you need the string you're trying to evaluate to be built
from the actual value of $ab:

eval(eval('3 + ' . $1))
eval('3 + $ab')
3 + 7
10

So change your s/// to:
$str=3D~s/(\$\w+)/'3 + ' . $1/eeg

Paul Lalli