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

A

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
 
P

Paul Lalli

Using the following example:

$ab=7;
$str='$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=~s/(\$\w+)/'3 + ' . $1/eeg

Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top