Skip to content

Commit 040a4d7

Browse files
committed
perlop: properly document s///e modifier
- s///e is not limited to expressions. The replacement part can contain any number of statements, like 'do { ... }'. - s///ee does not treat the RHS as a string. The RHS is parsed as a block (like s///e), but then the resulting string is eval()'d. - You can have more than two e's. Each additional 'e' wraps another eval() around the result.
1 parent 03f24b8 commit 040a4d7

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

pod/perlop.pod

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2534,9 +2534,9 @@ expression is used instead. See L<perlre> for further explanation on these.
25342534
Options are as with C<m//> with the addition of the following replacement
25352535
specific options:
25362536

2537-
e Evaluate the right side as an expression.
2538-
ee Evaluate the right side as a string then eval the
2539-
result.
2537+
e Evaluate the right side as a block of code.
2538+
ee Evaluate the right side as a block of code, then eval()
2539+
the resulting string.
25402540
r Return substitution and leave the original string
25412541
untouched.
25422542

@@ -2548,10 +2548,16 @@ as normal delimiters; the replacement text is not evaluated as a command.
25482548
If the I<PATTERN> is delimited by bracketing quotes, the I<REPLACEMENT> has
25492549
its own pair of quotes, which may or may not be bracketing quotes, for example,
25502550
C<s(foo)(bar)> or C<< s<foo>/bar/ >>. A C</e> will cause the
2551-
replacement portion to be treated as a full-fledged Perl expression
2551+
replacement portion to be treated as a full-fledged block of Perl code
25522552
and evaluated right then and there. It is, however, syntax checked at
2553-
compile-time. A second C<e> modifier will cause the replacement portion
2554-
to be C<eval>ed before being run as a Perl expression.
2553+
compile-time. A second C<e> modifier will cause the result of the block of code
2554+
to be C<eval>ed.
2555+
2556+
That is, C<s/FOO/BAR/e> will compute the replacement string at match time as if
2557+
it had been written as C<do { BAR }> (see L<perlfunc/do BLOCK>). Each
2558+
additional C<e> modifier wraps a call to C<eval> around the result:
2559+
C<s/FOO/BAR/ee> will compute the replacement string like C<eval(do { BAR })>,
2560+
C<s/FOO/BAR/eee> like C<eval(eval(do { BAR }))>, etc.
25552561

25562562
Examples:
25572563

@@ -2603,6 +2609,9 @@ Examples:
26032609
# to the variable name, and then evaluated
26042610
s/(\$\w+)/$1/eeg;
26052611

2612+
# Same as above, but perhaps clearer
2613+
s/(\$\w+)/eval($1)/eg;
2614+
26062615
# Delete (most) C comments.
26072616
$program =~ s {
26082617
/\* # Match the opening delimiter.

0 commit comments

Comments
 (0)