Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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:

$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.


    _($_=" "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?

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2015-11-26 04:32 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (696 votes), past polls