Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Loop Quandary

by Crackers2 (Parson)
on Nov 21, 2013 at 19:03 UTC ( #1063782=note: print w/replies, xml ) Need Help??


in reply to Loop Quandary

My guess is in the first loop the match happens on the same string each time, so you'll find each a in turn then exit the loop

In the second example, you call InfStr on every cycle of the loop, which makes it a regex against a new string, so it'll find the first a in the string each time and never progress

(Update: You can see this more clearly if you use (.) instead of (a) for the match; first loop will go through each letter, second on will always find the first a)

Replies are listed 'Best First'.
Re^2: Loop Quandary
by choroba (Archbishop) on Nov 21, 2013 at 23:45 UTC
    Nice explanation. Interestingly, adding the empty prototype to the sub definition and removing return changes the behaviour, as Perl optimizes the sub away:
    sub InfStr () { 'ababa' }

    which is equivalent to

    use constant INFSTR => 'ababa';
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2020-10-24 18:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (246 votes). Check out past polls.

    Notices?