Closed p5pRT closed 21 years ago
I found a bug\, or at least unexpected operation\, from the last operator inside a do-while loop.
% perl -v
This is perl\, version 5.005_03 built for i386-linux
Copyright 1987-1999\, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the GNU General Public License\, which may be found in the Perl 5.0 source kit.
Complete documentation for Perl\, including FAQ lists\, should be found on this system using `man perl' or `perldoc perl'. If you have access to the Internet\, point your browser at http://www.perl.com/\, the Perl Home Page.
I wrote some test code which demonstrates my unexpected output:
----- Begin test_dowhile.pl ----- #!/usr/bin/perl
print "Before For Loop.\n"; for( $i=0; $i\<2; $i++) { print " For loop iteration "\, $i\, "\n"; $j=0; print " Before do-while loop.\n"; do { print " Do-while iteration "\, $j\, "\n"; last if $j>1; $j++; } while( $j \< 10); print " After do-while loop.\n"; } print "After For Loop.\n"; ----- End test_dowhile.pl -----
I expected the following output:
Before For Loop. For loop iteration 0 Before do-while loop. Do-while iteration 0 Do-while iteration 1 Do-while iteration 2 After do-while loop. For loop iteration 1 Before do-while loop. Do-while iteration 0 Do-while iteration 1 Do-while iteration 2 After do-while loop. After For Loop.
Instead\, I got: Before For Loop. For loop iteration 0 Before do-while loop. Do-while iteration 0 Do-while iteration 1 Do-while iteration 2 After For Loop.
It appears that the "last" operator does not consider do-while a loop\, and therefore exits the parent loop. I've already re-written my code to use a for() loop\, and most people don't use do-while's\, so this is probably a low priority.
-- Craig Lewis CLewis@ApolloSolutions.Com -----BEGIN GEEK CODE BLOCK----- Version: 3.12 GE/CS d- s:+ a--@ C++$ UL++++$ P+++$ L+++(++) E--- W+++$ N+ o K w-- O M- V- PS+ PE+(-) Y+ PGP- t 5 X+ R tv b++(++++) DI+ D+ G+ e++>* h--- r+++ y+++ ------END GEEK CODE BLOCK------
On Thu\, Dec 16\, 1999 at 02:06:38PM -0600\, Craig Lewis wrote:
I did a quick search of the PerlBug Database\, but I didn't find anything that seems related.
I found a bug\, or at least unexpected operation\, from the last operator inside a do-while loop.
As documented. perldoc -f last
last cannot be used to exit a block which returns a value such as eval {}\, sub {} or do {}\, and should not be used to exit a grep() or map() operation.
Ronald
Migrated from rt.perl.org#1907 (status was 'resolved')
Searchable as RT1907$