Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Deep Recursion Limit

by greatdane (Beadle)
on Jan 27, 2004 at 23:06 UTC ( #324564=perlquestion: print w/replies, xml ) Need Help??
greatdane has asked for the wisdom of the Perl Monks concerning the following question:

Anyone know how to bump the deep recursion limit? I know how to turn off the warning (no warnings 'recursion'). Yet, I would rather keep the warning and raise the limit. I have code that is legitimately encountering the default limit (100 or so). It is perfectly fine code. All would be well if I could set the warning to 1000. Thanks for the help.

Replies are listed 'Best First'.
Re: Deep Recursion Limit
by broquaint (Abbot) on Jan 28, 2004 at 00:41 UTC
    This can be done, but not quite as simply as you had perhaps hoped
    use strict; use warnings; ## disable perl's warning mechanism no warnings 'recursion'; use B 'svref_2object'; use Symbol 'qualify_to_ref'; sub change_depth_warn { my($subname, $limit) = @_; my $subref = \&$subname; my $gv = svref_2object($subref)->GV; my $lineno = 0; no warnings 'redefine'; *{ qualify_to_ref $subname } = sub { if( $gv->CV->DEPTH % $limit == 0 ) { $lineno = do { my $i = 0; 1 while caller $i++; (caller($i - 2))[2] } unless $lineno; warn sprintf "Deep recursion on subroutine '%s' at %s line %d.\n", join('::', $gv->STASH->NAME, $gv->NAME), $0, $lineno; } &$subref(@_); }; } my $cnt = 0; sub foo { &foo while $cnt++ < $_[0] } my $maxdepth = 1000; my $recdepth = 3000; change_depth_warn('foo', $maxdepth); printf "calling foo(), expecting %d warnings ...\n", $recdepth / $maxdepth; foo($recdepth); __output__ calling foo(), expecting 3 warnings ... Deep recursion on subroutine 'main::foo' at perlmonks/ + line 43. Deep recursion on subroutine 'main::foo' at perlmonks/ + line 43. Deep recursion on subroutine 'main::foo' at perlmonks/ + line 43.
    So that disables the recursion warning, and then wraps the foo() in a closure that emits a recursion warning for every $limitth recursion.


      Post that on CPAN in something - anything. I can see great utility in that. Maybe as a pragma?
      use warnings::recursion 1000;

      We are the carpenters and bricklayers of the Information Age.

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Deep Recursion Limit
by diotalevi (Canon) on Jan 27, 2004 at 23:55 UTC

    Recompile your binary. This number is hardcoded into the perl source.

    pp_hot.c if (CvDEPTH(cv) == 100 && ckWARN(WARN_RECURSION) && !(PERLDB_SUB && cv == GvCV(PL_DBsub))) sub_crush_depth(cv);
    pp_ctl.c if (CvDEPTH(cv) == 100 && ckWARN(WARN_RECURSION)) sub_crush_depth(cv);
Re: Deep Recursion Limit
by rob_au (Abbot) on Jan 28, 2004 at 00:03 UTC
    In addition to the debugging and recompilation tips supplied by others above, I would recommend perhaps taking a look at the recursion algorithm itself - It may be more efficient to unroll the recursion employing a stack over which to iterate. For this task, you may find the threads Turning a recursive function into an iterator and Unrolling recursion of interest.


    perl -le "print unpack'N', pack'B32', '00000000000000000000001010110110'"

Re: Deep Recursion Limit
by PodMaster (Abbot) on Jan 28, 2004 at 00:00 UTC
    You'd have to "hack" perl (modify pp_hot.c ) and recompile to actually raise the limit. It's easier just to override the $SIG{__WARN__}. See `perldoc -f warn'.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Deep Recursion Limit
by ysth (Canon) on Jan 28, 2004 at 00:02 UTC
    No, its hardcoded (in two places, no less). You can rebuild perl with it changed, though (look for calls to sub_crush_depth in pp_ctl.c and pp_hot.c).
Re: Deep Recursion Limit
by Ao (Friar) on Jan 27, 2004 at 23:25 UTC
    I'm not sure if this will help, but from Programming Perl, 3rd ed:
        For the standard debugger, the $DB::deep variable (how many levels of recursion deep into the debugger you can go before a mandatory break) . . .

    Have you tried changing the value of that?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://324564]
Approved by neuroball
Front-paged by grinder
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2017-01-18 04:52 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (160 votes). Check out past polls.