http://www.perlmonks.org?node_id=932937


in reply to Deep recursion problem

Turning on use diagnostics; will give you a more meaningful warning, which may explain why there is a warning when you reach 100:

Deep recursion on subroutine "main::recurse" at mytest.pl line 11 (#1) (W recursion) This subroutine has called itself (directly or indirectly) 100 times more than it has returned. This probably indicates an infinite recursion, unless you're writing strange benchmark programs, in which case it indicates something else.

This threshold can be changed from 100, by recompiling the perl binary, setting the C pre-processor macro PERL_SUB_DEPTH_WARN to the desired value.

Keep in mind that recursion is often seen as a simple means of traversing a binary tree or doing binary searches; O(log n) operations for balanced trees. A tree size would have to be greater than 2.7e43 elements for you to reach the recursion max warning in an efficiently designed recursive tree-traversal or binary search algorithm. ...and when you exceed that many function calls on the call stack the speed of an iterative approach begins to weigh in favorably against recursion. Recursion can be used for many other things though, so Perl lets you lift the warning.


Dave