develooper Front page | perl.perl5.porters | Postings from December 1999

[ID 19991216.002] do-while bug

Thread Previous | Thread Next
From:
Craig Lewis
Date:
December 16, 1999 11:05
Subject:
[ID 19991216.002] do-while bug
Message ID:
3859464E.A63FC0A6@apollosolutions.com
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.

% 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------

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About