#!/usr/bin/perl # There is everything you need: # a sentinel, $f{0} = sub { 1 }; # a recursive function, sub f { $f{$n = pop()-1} ||= sub { f($n) }; ($n+1) * $f{$n}->(); } # a call to the recursive function... print f(shift);