Hello Monks,
I'll try to be brief. I'm a newbie trying to learn Perl with that cryptic tome, the Camel. I'm on "Fancy Patterns" and they are dealing with code within a regular expression. Can some enlightened Monk please, please explain this damn example??? Here it is:
$_ = "lothlorien";
m/ (?{ $i = 0 })
(. (?{ local $i = $i + 1; }) )*
lori
(?{ $result = $i })
/xi;
Now i = 4.
This example purportedly makes use of dynamic scoping. Unfortunately, the Camel has done a pretty crappy job explaining, or even using in examples, things like "local". The example right before this one was the exact same thing, but just used "$i++" instead of local, and didn't need the $result variable at the end. In that one, $i=10. A fellow monk explained to me that the .* greedily gobbles up the whole string, 10 characters, so $i gets incremented to 10 and the .* never matches again. (it only gives up some characters due to back-tracking). But in this one $i is 4!
I have no idea why, and my study has come to a grinding halt. Please help! Thanks :)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.