our $global_answer;

 

recursive_function (initial parameter list);

 

sub recursive_function {

 

      some stuff to do;

      do we have answer?

      if yes {

            $main::global_answer = ...

            return;

      else

            recursive_function (refined parameter list);

            return;

      end

}

 

 

 

  _____  

From: perl-win32-users-boun...@listserv.activestate.com
[mailto:perl-win32-users-boun...@listserv.activestate.com] On Behalf Of Greg
Aiken
Sent: Thursday, May 21, 2009 2:02 PM
To: perl-win32-users@listserv.ActiveState.com
Subject: question about recursion

 

given:

a. that its generally considered to be 'bad form' to use global variables
inside of sub-routines.

b. that I need to write a recursive sub-routine to solve a mathematical
problem.

 

the sub-routine will recursively call itself until the 'answer' is derived.
when the innermost call finishes executing, the program will drop through to
the previous call, and so forth.  until it drops out of the original first
call to the function.

 

at present, I am 'cheating' here by using a global variable to control this
recursive behavior.  when the innermost loop finds the 'answer', the global
variable is set to $weve_found_answer=1; 

 

the sub-routines then look to the value of $main::weve_found_answer to
determine if it should stop recursively calling itself because the answer
has already been found - or continue to recursively call itself again.

 

for now it 'works' but its not following the 'don't use globals unless there
is no better way' principle.

 

so I ask.  'is there a better way of allowing each sub routines
instantiation, at any nested level of the recursive call, be able to know
when the answer has been found?'

 

if anyone has any insights or experience with this, I would be very
interested to learn from your experience

 

thanks, greg

_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to