[sf-perl] a reason to goto

Joe Brenner doom at kzsu.stanford.edu
Sat Feb 24 10:48:32 PST 2007


Quinn Weaver <quinn at fairpath.com> wrote:

> Joe Brenner wrote:
> > 
> > I found a reason to use a goto!
> > 
> >   if ($RANDOMIZE) { 
> >     DO_OVER: do { 
> >       $i = pick_numeric(0, $numb_cards-1);
> >       $j = pick_numeric(0, $numb_cards-1);
> >       unless( $REFLEXIVE ) { 
> >         if ($i == $j) { 
> >           goto DO_OVER;
> >         }
> >       }
> >     };
> 
> Hmmn!
> 
> > My first thought was to just say "redo", but that's a runtime 
> > error: you can't use redo outside of a loop.  Not even inside a 
> > "do" block... which I think counts as another perl oddity.
> > 
> > Of course, this could be re-written something like this, but I 
> > wouldn't call it an improvement:
> > 
> >   if ($RANDOMIZE) { 
> >     do { 
> >       $i = pick_numeric(0, $numb_cards-1);
> >       $j = pick_numeric(0, $numb_cards-1);
> >       } until ( not( $REFLEXIVE ) && ($i != $j) )
> >     } 
> 
> Furthermore, wouldn't this cause an infinite loop if $REFLEXIVE is true?

Well you know, I said "*something* like this", not "exactly".  

None of my booleans are to be trusted without extensive testing.  
(I should put that in a .sig file.)


More information about the SanFrancisco-pm mailing list