in reply to /e in regexes

The "e" being absent means "treat the replace expression as a double-quote string literal".

$x = '10+4'; print( "$x" ); # 10+4, not 14

The "e" being present means "treat the replace expression as a Perl expression".
The Perl expression $1 evaluates to the value of $1, which is the string 10+4.

$x = '10+4'; print( $x ); # 10+4, not 14

s/.../.../ee is an awful* way of saying s/.../eval .../e.
That means that not only is $1 treated as a Perl expression, so is the value returned by it.

$x = '10+4'; print( eval $x ); # 14

* — In my opinion, something as dangerous as eval EXPR shouldn't be hidden as an simple little "e".

Update: Added illustrative code.

Replies are listed 'Best First'.
Re^2: /e in regexes
by waldner (Beadle) on Jun 22, 2008 at 16:44 UTC
    Thanks. But why a single /e is enough in the first case then?
      Because in this cases the evaluated string is "10" + 1, which forces perl to treat "10" as a number. If your evaluated string is "10+1", The whole thing is taken as a string literal.

      In one case, you want the replacement expression to be a Perl expression,
      In the other, you want the value of $1 to be evaluated.

      # s/(10)/$1+1/e $dollar1 = "10"; $replace = $dollar1 + 1; # One 'e', so Perl expression. print $replace; # 11 # s/(10\+4)/$1/e $dollar1 = "10+4"; $replace = $dollar1; # One 'e', so Perl expression. print $replace; # 10+4 # s/(10\+4)/$1/ee $dollar1 = "10+4"; $replace = eval $dollar1; # Two 'e', so Perl expression + eval. print $replace; # 14

      Update: Oops, this was supposed to be a reply to Re^2: /e in regexes.

        Thanks everybody. Thanks to this last example it's clear now (I hope).