Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Please tell me the meaning of this code.

by editi (Novice)
on Jul 01, 2007 at 09:16 UTC ( #624283=perlquestion: print w/ replies, xml ) Need Help??
editi has asked for the wisdom of the Perl Monks concerning the following question:

Monks,
$count = 0; s{((whom?)ever)}{ ++$count == 5 # is it the 5th? ? "${2}soever" # yes, swap : $1 # renege and leave it there }ige;
Thank you

Comment on Please tell me the meaning of this code.
Download Code
Replies are listed 'Best First'.
Re: Please tell me the meaning of this code.
by cLive ;-) (Parson) on Jul 01, 2007 at 09:34 UTC
    Change the 5th occurrence of whoever/whomever in $_ to whosoever/whomsoever. Here's a little more detail in a slight rewrite:
    my $count=0; $_ =~ s/ ((whom?)ever) # whom? is in $2, whom?ever is in $1 / if ( ++$count == 5 ) # for the fifth match { "${2}soever"; # set to whom?soever } else { $1; # leave it as it is } /igex; # case (i)nsensitive, (g)lobal match, (e)xecute replace +string. # (x) ignore whitespace (added for this code example)
    ps - I hope your tutor reads Perlmonks :)
Re: Please tell me the meaning of this code.
by shmem (Canon) on Jul 01, 2007 at 10:26 UTC
    This is basically a s/ubsti/tute/ operation (see Regexp Quote Like Operators operator, regexp in perlop) with the modifiers ige but in alternative syntax: the /patten/replacement/ elements get delimited with braces s{}{}, then the substitution is broken up with whitespace for better readability. The susbtitution part is actually a piece of code executed when the pattern is found, and which result gets interpolated for the pattern. That's what the e modifier stands for.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Please tell me the meaning of this code.
by ForgotPasswordAgain (Deacon) on Jul 02, 2007 at 10:06 UTC
    The meaning.... mu?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://624283]
Approved by varian
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2016-02-10 01:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?





    Results (330 votes), past polls