File: recursion.pl

package info (click to toggle)
libsoap-lite-perl 1.27-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 1,680 kB
  • sloc: perl: 8,908; makefile: 23; cs: 16
file content (40 lines) | stat: -rw-r--r-- 1,075 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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 = 10;
																							my $recdepth = 30;
																							change_depth_warn('foo', $maxdepth);

																							printf "calling foo(), expecting %d warnings ...\n",
																							       $recdepth / $maxdepth;

																								   foo($recdepth);