Note that there are some explanatory texts on larger screens.

plurals
  1. POMisunderstanding perl regexp evaluation
    primarykey
    data
    text
    <p>Good time of day! I am reading a book about perl: "Programming Perl" By Larry Wall, Tom Christiansen, Jon Orwant. In this book I found several examples that were not clarified by the authors (or simply I dont get then). </p> <h2>The first</h2> <p>This prints hi only ONCE.</p> <pre><code> "adfsfloglig"=~ /.*(?{print "hi"})f/; </code></pre> <p>But this prints "hi" TWICE?? how can it be explained? </p> <pre><code> "adfsfloglig"=~ /.*(?{print "hi"})log/; </code></pre> <p>And continuing to experement even make things worse: </p> <pre><code> "adfsfloglig"=~ /.*(?{print "hi"})sflog/; </code></pre> <p>The above string of code again prints only ONCE this terrifying "hi" ! After about a week I understood only one thing completely - I NEED HELP :) SO I am asking you to help me, please.</p> <h2>The second (this is a bomb!)</h2> <pre><code> $_ = "lothiernbfj"; m/ (?{$i = 0; print "setting i to 0\n"}) (.(?{ local $i = $i + 1; print "\ti is $i"; print "\tWas founded $&amp;\n" }))* (?{print "\nchecking rollback\n"}) er (?{ $result = $i; print "\nsetting result\n"}) /x; print "final $result\n"; </code></pre> <p>Here the <code>$result</code> finally printing on the screen is equal to number of chars that were matched by <code>.*</code>, but I don't get it again. </p> <p>When turning on debug printing(shown above), i see, that <code>$i</code> is being incremented every time the new char is included in <code>$&amp;</code> (matched part of a string). </p> <p>In the end <code>$i</code> is equal 11 (amount of chars in a string), then there are 7 rollbacks, when <code>.*</code> returns from its match char at a time (7 times) so the match of an all pattern occurs. </p> <p>But, damn magic, the result is setting to value of <code>$i</code>! And we were not decrementing this value anywhere! So <code>$result</code> should be equal 11! But it is not. And authors were right. I know.</p> <p>Please, can you explain this strange perl code, i was happy to met? Thank you for any answer! </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload