| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Context::Preserve; # git description: a897cd7 |
|
2
|
|
|
|
|
|
|
# ABSTRACT: Run code after a subroutine call, preserving the context the subroutine would have seen if it were the last statement in the caller |
|
3
|
2
|
|
|
2
|
|
31038
|
use strict; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
46
|
|
|
4
|
2
|
|
|
2
|
|
9
|
use warnings; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
47
|
|
|
5
|
2
|
|
|
2
|
|
10
|
use Carp; |
|
|
2
|
|
|
|
|
6
|
|
|
|
2
|
|
|
|
|
120
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
9
|
use base 'Exporter'; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
489
|
|
|
8
|
|
|
|
|
|
|
our @EXPORT = qw(preserve_context); |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub preserve_context(&@) { |
|
13
|
9
|
|
|
9
|
1
|
1080
|
my $orig = shift; |
|
14
|
9
|
|
|
|
|
26
|
my %args = @_; |
|
15
|
|
|
|
|
|
|
|
|
16
|
9
|
|
|
|
|
16
|
my $replace = $args{replace}; |
|
17
|
9
|
|
|
|
|
13
|
my $after = $args{after}; |
|
18
|
|
|
|
|
|
|
|
|
19
|
9
|
100
|
100
|
|
|
53
|
croak 'need an "after" or "replace" coderef' |
|
20
|
|
|
|
|
|
|
unless $replace || $after; |
|
21
|
|
|
|
|
|
|
|
|
22
|
8
|
50
|
|
|
|
27
|
if(!defined wantarray){ |
|
|
|
100
|
|
|
|
|
|
|
23
|
0
|
|
|
|
|
0
|
$orig->(); |
|
24
|
0
|
0
|
|
|
|
0
|
if($after){ |
|
25
|
0
|
|
|
|
|
0
|
$after->(); |
|
26
|
|
|
|
|
|
|
} |
|
27
|
|
|
|
|
|
|
else { |
|
28
|
0
|
|
|
|
|
0
|
$replace->(); |
|
29
|
|
|
|
|
|
|
} |
|
30
|
0
|
|
|
|
|
0
|
return; |
|
31
|
|
|
|
|
|
|
} |
|
32
|
|
|
|
|
|
|
elsif(wantarray){ |
|
33
|
4
|
|
|
|
|
10
|
my @result = $orig->(); |
|
34
|
4
|
100
|
|
|
|
30
|
if($after){ |
|
35
|
3
|
|
|
|
|
8
|
my @ignored = $after->(@result); |
|
36
|
|
|
|
|
|
|
} |
|
37
|
|
|
|
|
|
|
else { |
|
38
|
1
|
|
|
|
|
3
|
@result = $replace->(@result); |
|
39
|
|
|
|
|
|
|
} |
|
40
|
4
|
|
|
|
|
48
|
return @result; |
|
41
|
|
|
|
|
|
|
} |
|
42
|
|
|
|
|
|
|
else { |
|
43
|
4
|
|
|
|
|
9
|
my $result = $orig->(); |
|
44
|
4
|
100
|
|
|
|
26
|
if($after){ |
|
45
|
3
|
|
|
|
|
8
|
my $ignored = $after->($result); |
|
46
|
|
|
|
|
|
|
} |
|
47
|
|
|
|
|
|
|
else { |
|
48
|
1
|
|
|
|
|
3
|
$result = $replace->($result); |
|
49
|
|
|
|
|
|
|
} |
|
50
|
4
|
|
|
|
|
31
|
return $result; |
|
51
|
|
|
|
|
|
|
} |
|
52
|
|
|
|
|
|
|
} |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
1; |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
__END__ |