From: perl-win32-users-boun...@listserv.activestate.com [mailto:perl-win32-users-boun...@listserv.activestate.com] On Behalf Of Greg Aiken Sent: 21 May 2009 22:02 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. Make that "avoid globals as much as possible". > b. that I need to write a recursive sub-routine to solve a mathematical problem. It isn't uncommon for mathematical problems that have a mathematical solution that is recursive to be more effectively solved computationally using iteration. > > 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 Without more detail it is hard to say (some example code would be useful here). However, you should be able to use the return value from your recursive sub, rather than a global. HTH -- Brian Raven This e-mail may contain confidential and/or privileged information. If you are not the intended recipient or have received this e-mail in error, please advise the sender immediately by reply e-mail and delete this message and any attachments without retaining a copy. Any unauthorised copying, disclosure or distribution of the material in this e-mail is strictly forbidden. _______________________________________________ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs