Regular Expressions   «Prev 

Perl e Substitution 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;

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:

Regular Expressions
1) The user is referring to a variable name $money
The user is referring to a variable name $money. so the substitution must evaluate this as code instead of as a string.

2) This line of code will do the substitution
This line of code will do the substitution

3) Matches the dollar sign in before money
Matches the dollar sign in before money

4) matches any word that comes after the dollar sign.
matches any word that comes after the dollar sign. The brackets tell the Perl program to remember what matched the \w+ and stores that in the variable $l.

5) refers to the first item in parentheses in a matching string
refers to the first item in parentheses in a matching string

6) makes the item a scalar variable
makes the item a scalar variable

7) tells the Perl program to treat the resulting string as a Perl variable, not a literal string
tells the Perl program to treat the resulting string as a Perl variable, not a literal string

8) The result is the Perl variable money
The result is the Perl variable money