Sometimes a deeply recursive method is the easiest solution to a problem, but if you recurse more than a couple of hundred calls you'll hit perl's "deep recursion" error. Here's a snippet that allows you to tail recurse (where the result of the recursive call is returned directly) without eating stack.