Lesson 8 | Perl e modifier |
Objective | 'e' modifier is placed at the end of the substitution expression. |
Perl e modifier
The e
modifier is used if you want the right-hand side of the substitution to be evaluated as code, instead of being treated as a string.
For example,
s/$(\w+)//e;
If there is a need to substitute the substring with a replacement string which is a regular expression to be evaluated,
the 'e' modifier is used. The 'e' modifier is placed at the end of the substitution expression.
s/To_be_replaced/Regular_Expression/e;
That will replace anything that looks like a Perl scalar variable with the contents of that variable.
This is extremely useful for embedding values in a text file.
Let us look at this example more closely:
Perl e-substitution Modifier