Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: perldoc of s///ee wrong or just misleading? (read more)

by Anonymous Monk
on Dec 28, 2014 at 20:52 UTC ( [id://1111588]=note: print w/replies, xml ) Need Help??


in reply to perldoc of s///ee wrong or just misleading?

Yes the wording is odd, but like a lot of perl docs, if you keep reading you will get all the details, like: A second e modifier will cause the replacement portion to be evaled before being run as a Perl expression.

I once wrote

s/regex/STRING /e
means replace that matched by regex with string
s/regex/CODEHERE/e
means replace that matched by regex with result of code; the e in s///e tells the s///ubstitution operator that the s//CODEHERE/e is code and not a string
s/regex/CODEHERE/e
means treat CODEHERE string as code
s/regex/CODEHERE/ee
means treat CODEHERE string as code, and treat the return value of that code as code
s/regex/CODEHERE/ee
means s/regex/eval CODEHERE/e

One time I even tried to explain this way :)

my $string = 'string'; ## s/string/replacement/; $string->substitute( 'pattern', 'replacement' ); ## s/string/codehere/e; $string->substitute( 'pattern', sub { ... } ); ## s/string/codehere/ee; $string->substitute( 'pattern', sub { eval ... } ); ## s/string/codehere/eee; $string->substitute( 'pattern', sub { eval eval ... } );

Replies are listed 'Best First'.
Re^2: perldoc of s///ee wrong or just misleading? (read more)
by ikegami (Patriarch) on Dec 29, 2014 at 18:05 UTC

    Consider

    s/.../$1' $2"/

    More accurate:

    $string->substitute( qr/pattern/, sub { qq/replacement/ } );

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1111588]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-23 06:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found