#!/usr/bin/perl -w $recur = 0; sub loop($) { my ($func) = @_; sub { local $recur = sub { my @args = @_; sub { $func->(@args); } }; my $ret = $func->(@_); while ('CODE' eq ref($ret)) { $ret = $ret->(); } $ret; }; }
Example usage:
my $foo = loop sub { my ($n, $a) = @_; if ($n == 0) { $a; } else { $recur->($n-1, $a*$n); } }; print $foo->(999999,1) . "\n";
No comments:
Post a Comment