>>>>> "PL" == Paul Lalli <> writes:
PL> Recursion is not implemented by a language, it's implemented by a
PL> program. To my knowledge there is no difference using recursion in a
PL> Perl program from using recursion in a program written in any other
PL> language.
that is slightly wrong. recursion is a language feature in that a
language must handle a stack for the recursive calls. some older langs
did not do this and you could only call a sub once and had to return
before calling it again. other langs had to declare a sub would be
recursive so they could compile in the stack stuff (regular calls were
different internally than recursive calls/subs). and with any language
you can always fake recursion with a loop and your own stack. in some
cases this is not a bad solution and it can be much faster than real
recursion.
PL> This is a subroutine declaration. It's main use is to notify Perl
PL> that a subroutine with this name and this prototype will be
PL> declared eventually. This enables the programmer to call the
PL> subroutine before being defined. There are only two circumstances
PL> in which this is needed: 1) The subroutine involves a prototype;
PL> 2) You want to call the subroutine without using parentheses
PL> around the arguments.
and in the cool hacks code since he was recursively calling his
prototyped sub, the compiler had to compile a declared prototype so it
could be refered to inside the sub. the sub's prototype couldn't be used
inside the sub for a declaration as it happens too late. but as the OP
noted, damian and others eschew prototypes so that makes this 'cool'
hack much less cool to me.
PL> It's not. The author is mistaken. There is no need to use a
PL> subroutine declaration just because that subroutine will be called
PL> recursively. Indeed, with no code of any kind between that declaration
PL> and definition, I'm 99% sure you could remove the declaration entirely
PL> without affecting anything else in the program.
it is just the prototype that forces the need for the predeclaration.
PL> Note that if we were to use a prototype on the above subroutine, then
PL> we would need to either 1) move the call to the subroutine below the
PL> subroutine itself, or 2) use a subroutine declaration above the call.
PL> This, however, is true of *any* prototyped subroutine, and has nothing
PL> to do with recursion.
well, you can define a prototyped sub and not need a separate prototype
declaration as long as all callers are later in the source and so the
compiler will have seen the prototype. it is the 'cool' code's use of
prototypes AND recursion that requires the predeclared prototype for the
sub's own use.
uri
--
Uri Guttman ------
--------
http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ----------------------------
http://jobs.perl.org