An Introduction to Regular Expressions The Substitution Operator 34 of 50 : [PREV] [NEXT]

The /e Modifier

Some REPLACEMENT strings may need to be evaluated before the REPLACEMENT string is fully complete.

Consider the following by Randal Schwartz:

For many years, I used to end my postings in comp.lang.perl.misc with some clever (often obscure) chunk of code that would print out "Just another Perl hacker,". When I discovered the "double eval" trick for substution, I just had to use it in one of these "JAPH" postings. And here's the result:

$Old_MacDonald = q#print #; $had_a_farm = (q-q:Just another Perl hacker,:-);
s/^/q[Sing it, boys and girls...],$Old_MacDonald.$had_a_farm/eieio;

See if you can figure out how it works!

It actually just prints:

Just another Perl hacker,

© 2003 Barbie barbie@missbarbell.co.uk Home http://birmingham.pm.org/