[sf-perl] a reason to goto

Joe Brenner doom at kzsu.stanford.edu
Sat Feb 24 11:09:43 PST 2007


Joe Brenner <doom at kzsu.stanford.edu> 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;
>         }
>       }
>     };
> 
> 
> 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.

Reviewing the docs, this looks like the correct way of saying "I've
never needed to use a goto in my life":

   my $count = 0;
   if ($RANDOMIZE) { 
     {{ 
       $i = pick_numeric(0, $numb_cards-1);
       $j = pick_numeric(0, $numb_cards-1);
       unless( $REFLEXIVE ) { 
         if ($i == $j) { 
           redo;
         }
       }
     }}
   }
   
The extra brackets create a loop which is excuted only once. 
That's clear, right? 



More information about the SanFrancisco-pm mailing list