-
Notifications
You must be signed in to change notification settings - Fork 591
perlop: properly document s///e modifier #23656
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: blead
Are you sure you want to change the base?
Conversation
- 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.
If it behaves as a |
Yes: use v5.36;
sub Honk::DESTROY {
say "$_[0] destroyed";
}
my $string = "hello";
$string =~ s{([eo])}{
say "replacing '$1'";
my $obj = bless {}, 'Honk';
"_"
}eg;
say "done: $string";
say '$obj is ', eval('$obj') // "not in scope: $@"; Output:
Only in the sense that every |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This p.r. is a huge improvement IMO
to be C<eval>ed. | ||
|
||
That is, C<s/FOO/BAR/e> will compute the replacement string at match time as if | ||
by C<do { BAR }> (see L<perlfunc/do BLOCK>). Each additional C<e> modifier |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "by" here confuses me. Maybe "as if it had been written as"
That is, C<s/FOO/BAR/e> will compute the replacement string at match time as if | ||
by C<do { BAR }> (see L<perlfunc/do BLOCK>). Each additional C<e> modifier | ||
wraps a call to C<eval> around the result: C<s/FOO/BAR/ee> will compute the | ||
replacement string as if by C<eval(do { BAR })>, C<s/FOO/BAR/eee> as if by |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, the by
doesn't sound good to me
result. | ||
e Evaluate the right side as a block of code. | ||
ee Evaluate the right side as a block of code, then eval() | ||
the resulting string. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where exactly in the source code does this evaluation of the RHS as a block of code rather than as either an expression
or a string
is enforced? (I'm asking this not to quibble, but just to know your reference point.)
And I wrote a perfect example years ago:
|
s///e
is not limited to expressions. The replacement part can contain any number of statements, likedo { ... }
.s///ee
does not treat the RHS as a string. The RHS is parsed as a block (likes///e
), but then the resulting string iseval()
d.e
s. Each additionale
wraps anothereval()
around the result.