Robert's Perl Tutorial

http://www.sthomas.net/roberts-perl-tutorial.htm


/ee

It is even possible to have more than one /e . For example:

$data='The function is <5funcA>';

$funcA='*2+4';

print "$data\n";

$data=~s/<(\d)(\w+)>/($1+2).${$2}/;	# first time
# $data=~s/<(\d)(\w+)>/($1+2).${$2}/e;	# second time
# $data=~s/<(\d)(\w+)>/($1+2).${$2}/ee;	# third time

print "$data\n";

To properly appreciate this you need to run it three times, each time commenting out a different line. Only one regex line should be uncommented when the program is run.

The first time round the regex is a dumb variable interpolation. Perl just searches the string for any variables, finds $1 and $2, and replaces them.

Second time round the expression is evaluated, as opposed to just plain variable-interpolated. This means that $1+2 is evaluated. $1 has a value of 5, pl, plus 2 == 7. The other part of the replacement, ${$2} is evaluated only so far as working out that the variable named $2 should be placed in the string.

Third time round and Perl now makes a second pass through the string, looking for things to do. After the first pass, and just before that second pass the string looks like this; 7*2+4 . Perl evaluates this, and prints the result.

So the more /e 's you add on the end of the regex, the more passes Perl makes through the replacement string trying to evaluate the code.

This is fairly advanced stuff here, and it is probably not something you will use every day. But knowing it is there is handy.