[sf-perl] a reason to goto

Neil Heller nheller at silcon.com
Sun Feb 25 15:00:03 PST 2007

If I remember correctly (and I may be wrong) this discussion came up with
reference to using "goto" to break out of recursive algorithm.  If I'm
right, what would be the remaining state of the program's internal stack?

Neil Heller
nheller at silcon.com
(510) 862-4387

-----Original Message-----
From: sanfrancisco-pm-bounces+nheller=silcon.com at pm.org
[mailto:sanfrancisco-pm-bounces+nheller=silcon.com at pm.org] On Behalf Of Joe
Sent: Saturday, February 24, 2007 11:25 PM
To: San Francisco Perl Mongers User Group
Subject: Re: [sf-perl] a reason to goto

David Alban wrote:

> What about making a subroutine out of it, to which you pass $REFLEXIVE 
> (and maybe $i and $j)?  If after calling pick_numeric( ... ) 
> $REFLEXIVE doesn't have the value you want, you recursively call the 
> subroutine?  I know some folks don't like recursion, and if you 
> anticipate a large number of recursive calls it could be a performance 
> issue, but I thought I'd ask.

I don't have any particular dislike of recursion [1] -- except that I find
that it *still* takes me forever to debug recursive code -- but I probably
wouldn't resort to it for something like this.  The code as 
written strikes me as fairly straight-forward, it's just that perl's 
quirks were getting in the way of getting it working. 

In case it's not clear, the idea is simply that I want to randomly choose
two integers, but under normal conditions I'd rather that 
they not be the same numbers (however if $REFLEXIVE is set, then 
two identical picks are okay).  

I don't know if it made it out to the list (I think my mail is acting flaky
at the moment) but the perlish way of doing it is probably this:

   if ($RANDOMIZE) {
       $i = pick_numeric(0, $numb_cards-1);
       $j = pick_numeric(0, $numb_cards-1);
       unless( $REFLEXIVE ) {
         if ($i == $j) {

Though if "pick_numeric" were an expensive function, I might prefer not to
redo both picks, and I'd go for *something* like this:

   if ($RANDOMIZE) {
       $i = pick_numeric(0, $numb_cards-1);
       do { 
          $j = pick_numeric(0, $numb_cards-1);       
       } until ($j != $i) || ($REFLEXIVE);

[1] As for worrying about disk thrashing: it's not particuarly difficult to
use a counter to error out if you've exceeded 10000 levels of recursion or

SanFrancisco-pm mailing list
SanFrancisco-pm at pm.org http://mail.pm.org/mailman/listinfo/sanfrancisco-pm

More information about the SanFrancisco-pm mailing list