in reply to private recursive subroutines
You can get around that by simply declaring $func as an undefined lexical and then re-defining it as a subroutine. Since the recursive call doesn't get executed until the sub is called for the first time, by that time, $func will contain the code reference. For example,
my $fac; $fac = sub { my ( $n ) = @_; return 1 if $n == 1; return $n * $fac->( $n - 1 ); }; print $fac->( 5 );
Yes, I know factorials are a bad example for recursion; it's just the first thing that came to mind. :)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: private recursive subroutines
by blokhead (Monsignor) on May 10, 2007 at 16:36 UTC | |
by Sixtease (Friar) on May 10, 2007 at 21:10 UTC | |
|
Re^2: private recursive subroutines
by Sixtease (Friar) on May 10, 2007 at 15:58 UTC | |
|
Re^2: private recursive subroutines
by naikonta (Curate) on May 10, 2007 at 15:51 UTC |