Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: Deep Recursion Limit

by broquaint (Abbot)
on Jan 28, 2004 at 00:41 UTC ( #324586=note: print w/replies, xml ) Need Help??

in reply to Deep Recursion Limit

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.


Replies are listed 'Best First'.
Re: Re: Deep Recursion Limit
by dragonchild (Archbishop) on Jan 28, 2004 at 03:59 UTC
    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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://324586]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2017-01-22 21:57 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (190 votes). Check out past polls.