in reply to Re^2: Reference to an anon sub from within.
in thread Reference to an anon sub from within.
Here is my recursive 'sort-of' anonymous routine that calculates factors
although, by using $sub it's not really anonymous, is it?# --- Factoring with a nony mouse sub { my $sub; my $fact; $fact = 1; $sub = sub {my $num=shift; $fact=$num*$fact; if ($num==1){print "$fact\n"} else{$num--;$sub->($num)}; return $sub}; $sub->(5); }
UPDATE
Improved version of above
# --- Factoring with a nony mouse sub my $sub; { my $fact; $fact = 1; $sub = sub {my $num=shift; $fact=$num*$fact; $num--; $sub->($num) unless $num==1; return $fact}; } print $sub->(5),"\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Reference to an anon sub from within.
by tilly (Archbishop) on Mar 08, 2005 at 23:43 UTC |