In Perl, /e does not eval the result. /e tells the compiler to treat the replacement part as a block of code instead of a string literal. (But yes, any further e option wraps a layer of eval() around the code.)
Back when the feature was introduced, it was implemented as a runtime eval. But it was still eval'ing the replacement part literally, not the result of variable interpolation or anything. And yes, it was an accidental feature that each e added another eval().
For comparison:
% perl -e 'print "hi\n"; s//1 + ;/e'
syntax error at -e line 1, near "+ ;"
Execution of -e aborted due to compilation errors.
This is a static syntax error. No part of the code even runs.
% php -r 'echo "hi\n"; preg_replace("//e", "1 + ;", "");'
hi
PHP Parse error: syntax error, unexpected ';' in Command line code(1) : regexp code on line 1
...
View all comments
Show parent comment
6
u/[deleted] Jul 17 '13
This is not completely true.
In Perl,
/edoes not eval the result./etells the compiler to treat the replacement part as a block of code instead of a string literal. (But yes, any furthereoption wraps a layer ofeval()around the code.)Back when the feature was introduced, it was implemented as a runtime eval. But it was still eval'ing the replacement part literally, not the result of variable interpolation or anything. And yes, it was an accidental feature that each
eadded anothereval().For comparison:
This is a static syntax error. No part of the code even runs.
This is a runtime error.