View Source:
PerlLoopIncrement
Note:
This page has been locked and cannot be edited.
Recently I came across a curious bug in a perl script (naturally I found the bug via [code review|http://sysadvent.blogspot.com/2010/12/day-5-why-arent-you-doing-code-reviews.html]). Here's a code snippet of what got me thinking: <verbatim> for $i (1..4) { # do something $i++; } </verbatim> Wait, what? I knew from using the script previously that it definitely executed the loop four times. So what's up with that extra <code>$i++</code> in there? Shouldn't that cause the loop to only be run twice? I tried some sample code to figure it out: <verbatim> for $i (1..4) { print "before: $i\n"; $i++; print "after: $i\n"; } </verbatim> which results in: <verbatim> before: 1 after: 2 before: 2 after: 3 before: 3 after: 4 before: 4 after: 5 </verbatim> that demonstrates <code>$i</code> was being incremented by the <code>$i++</code>, but that was being thrown away on each iteration of the loop. Compare to this behavior in a traditional for loop, which is what I expected: <verbatim> for ($i=1;$i<5;$i++) { print "before: $i\n"; $i++; print "after: $i\n"; } </verbatim> which results in: <verbatim> before: 1 after: 2 before: 3 after: 4 </verbatim> that demonstrates <code>$i</code> is being incremented by the <code>$i++</code> and that incremented value is the starting value for the next iteration of the loop. Thus the loop only gets executed twice, as expected. I think this behavior has to do with how the range operator in the first example works. It evaluates to the list 1,2,3,4, and the number of iterations of the loop is then fixed. In this case it really is a <code>foreach</code> loop (although I know that <code>for</code> and <code>foreach</code> are the same thing in Perl). However, the second example is a true for loop so that incrementing of <code>$i</code> inside the loop is taken into account. I welcome a better explanation of this behavior if anyone has one. Does the precise answer have something to do with variable scope? Also you might wonder where this weird construct came from. I think I know: the original author started with a while loop, like this: <verbatim> while($i < 5) { # do something $i++; } </verbatim> but he then switched to a for loop and forgot to remove the extra increment operator for $i. So anyway, that's my observation of an odd perl bug that made me scratch my head for a moment. Also do note this is another great reason why sysadmins should do code review. ----- CategoryGeekStuff CategoryPerl CategoryBlog
Please enable JavaScript to view the
comments powered by Disqus.
HollenbackDotNet
Home Page
Popular Pages
All Categories
Main Categories
General Interest
Geek Stuff
DevOps
Linux Stuff
Pictures
Search
Toolbox
RecentChanges
RecentNewPages
What links here
Printable version
AllPages
RecentChanges
Recent Changes Cached
No changes found
Favorite Categories
ActionPage
(150)
WikiPlugin
(149)
GeekStuff
(137)
PhpWikiAdministration
(102)
Help/PageList
(75)
Help/MagicPhpWikiURLs
(75)
Blog
(69)
Pictures
(60)
GeneralInterest
(44)
LinuxStuff
(38)
Views
View Page
View Source
History
Diff
Sign In