File Coverage

inc/Sub/Uplevel.pm
Criterion Covered Total %
statement 31 67 46.2
branch 8 30 26.6
condition 0 5 0.0
subroutine 8 12 66.6
pod 1 1 100.0
total 48 115 41.7


line stmt bran cond sub pod time code
1             #line 1
2 2     2   62 package Sub::Uplevel;
  2         8  
  2         92  
3 2     2   20 use 5.006;
  2         18  
  2         190  
4             use strict;
5             # ABSTRACT: apparently run a function in a higher stack frame
6             our $VERSION = '0.24'; # VERSION
7              
8             # Frame check global constant
9             our $CHECK_FRAMES;
10 2     2   47 BEGIN {
11             $CHECK_FRAMES = !! $CHECK_FRAMES;
12 2     2   43 }
  2         4  
  2         436  
13             use constant CHECK_FRAMES => $CHECK_FRAMES;
14              
15             # We must override *CORE::GLOBAL::caller if it hasn't already been
16             # overridden or else Perl won't see our local override later.
17              
18             if ( not defined *CORE::GLOBAL::caller{CODE} ) {
19             *CORE::GLOBAL::caller = \&_normal_caller;
20             }
21              
22             # modules to force reload if ":aggressive" is specified
23             my @reload_list = qw/Exporter Exporter::Heavy/;
24              
25 2     2   14 sub import {
  2         6  
  2         448  
26 2     2   5 no strict 'refs'; ## no critic
27 2         6 my ($class, @args) = @_;
28 4 50       17 for my $tag ( @args, 'uplevel' ) {
    0          
29 4         7 if ( $tag eq 'uplevel' ) {
30 4         8 my $caller = caller(0);
  4         21  
31             *{"$caller\::uplevel"} = \&uplevel;
32             }
33 0         0 elsif( $tag eq ':aggressive' ) {
34             _force_reload( @reload_list );
35             }
36 0         0 else {
37             die qq{"$tag" is not exported by the $class module\n}
38             }
39 2         52 }
40             return;
41             }
42              
43 2     2   14 sub _force_reload {
  2         3  
  2         1678  
44 0     0   0 no warnings 'redefine';
45 0         0 local $^W = 0;
46 0         0 for my $m ( @_ ) {
47 0         0 $m =~ s{::}{/}g;
48 0 0       0 $m .= ".pm";
49             require $m if delete $INC{$m};
50             }
51             }
52              
53              
54             # @Up_Frames -- uplevel stack
55             # $Caller_Proxy -- whatever caller() override was in effect before uplevel
56             our (@Up_Frames, $Caller_Proxy);
57              
58 0     0   0 sub _apparent_stack_height {
59 0         0 my $height = 1; # start above this function
60 0 0       0 while ( 1 ) {
61 0         0 last if ! defined scalar $Caller_Proxy->($height);
62             $height++;
63 0         0 }
64             return $height - 1; # subtract 1 for this function
65             }
66              
67             sub uplevel {
68 0     0 1 0 # Backwards compatible version of "no warnings 'redefine'"
69 0         0 my $old_W = $^W;
70             $^W = 0;
71              
72 0 0       0 # Update the caller proxy if the uplevel override isn't in effect
73             local $Caller_Proxy = *CORE::GLOBAL::caller{CODE}
74 0         0 if *CORE::GLOBAL::caller{CODE} != \&_uplevel_caller;
75             local *CORE::GLOBAL::caller = \&_uplevel_caller;
76              
77 0         0 # Restore old warnings state
78             $^W = $old_W;
79 0         0  
80             if ( CHECK_FRAMES and $_[0] >= _apparent_stack_height() ) {
81             require Carp;
82             Carp::carp("uplevel $_[0] is more than the caller stack");
83             }
84 0         0  
85             local @Up_Frames = (shift, @Up_Frames );
86 0         0  
87 0         0 my $function = shift;
88             return $function->(@_);
89             }
90              
91 21     21   33801 sub _normal_caller (;$) { ## no critic Prototypes
92 21         26 my ($height) = @_;
93 21         124 $height++;
94 21 100       75 my @caller = CORE::caller($height);
95             if ( CORE::caller() eq 'DB' ) {
96             # Oops, redo picking up @DB::args
97 2         18 package DB;
98             @caller = CORE::caller($height);
99             }
100 21 50       50  
101 21 100       277 return if ! @caller; # empty
102 6 100       2682 return $caller[0] if ! wantarray; # scalar context
103             return @_ ? @caller : @caller[0..2]; # extra info or regular
104             }
105              
106 0   0 0     sub _uplevel_caller (;$) { ## no critic Prototypes
107             my $height = $_[0] || 0;
108              
109             # shortcut if no uplevels have been called
110             # always add +1 to CORE::caller (proxy caller function)
111 0 0         # to skip this function's caller
112             return $Caller_Proxy->( $height + 1 ) if ! @Up_Frames;
113              
114 0            
115 0           my $saw_uplevel = 0;
116             my $adjust = 0;
117              
118             # walk up the call stack to fight the right package level to return;
119             # look one higher than requested for each call to uplevel found
120             # and adjust by the amount found in the Up_Frames stack for that call.
121             # We *must* use CORE::caller here since we need the real stack not what
122             # some other override says the stack looks like, just in case that other
123             # override breaks things in some horrible way
124 0            
125 0           for ( my $up = 0; $up <= $height + $adjust; $up++ ) {
126 0 0 0       my @caller = CORE::caller($up + 1);
127             if( defined $caller[0] && $caller[0] eq __PACKAGE__ ) {
128             # add one for each uplevel call seen
129 0           # and look into the uplevel stack for the offset
130 0           $adjust += 1 + $Up_Frames[$saw_uplevel];
131             $saw_uplevel++;
132             }
133             }
134              
135             # For returning values, we pass through the call to the proxy caller
136 0           # function, just at a higher stack level
137 0 0         my @caller = $Caller_Proxy->($height + $adjust + 1);
138             if ( CORE::caller() eq 'DB' ) {
139             # Oops, redo picking up @DB::args
140 0           package DB;
141             @caller = $Sub::Uplevel::Caller_Proxy->($height + $adjust + 1);
142             }
143 0 0          
144 0 0         return if ! @caller; # empty
145 0 0         return $caller[0] if ! wantarray; # scalar context
146             return @_ ? @caller : @caller[0..2]; # extra info or regular
147             }
148              
149              
150             1;
151              
152             __END__