blib/lib/Safe/Caller.pm

filter: Statementstmt 100% 57 / 57 Branchbran 60.0% 12 / 20 Conditioncond 40.0% 4 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 12 / 12 Podpod 100% 5 / 5 totaltotal 81.8% 90 / 110 CC 18 SCAR 13.5
CC
18
Cov
84%
new
7.1
called_from_line
4.8
called_from_package
3.1
CRAP 3.9
« Build ↑ Summary
1 package Safe::Caller;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use boolean qw(true false);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our $VERSION = '0.09';
10  
11 2 ▶use constant FRAMES => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub new
14 {
15 1 ▶ my $class = shift;
Subroutine new: called CC=7 SCAR=19.6
Pod documented
16 1 my ($frames) = @_;
17 1 ▶ $frames ||= FRAMES;
Condition50%1/2
$frames ||= 1
l!l
01
MC/DC0%0/1
$frames ||= 1
$frames
Truth table50%1/2
A
A= $frames
Aresult
11
00
18  
19 my $caller = sub
20 {
21 14 ▶ my ($f, $elem) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.1
22 14 ▶ my $frames = defined $f ? $f : $frames;
Branch: 1 branch
BranchTrueFalseTotal
defined $f ? : 2 12 14
23 14 ▶ return (caller($frames + 2))[$elem] || '';
Condition50%1/2
(caller $frames + 2)[$elem] || ''
l!l
140
MC/DC0%0/1
(caller $frames + 2)[$elem] || ''
(caller $frames + 2)[$elem]
Truth table50%1/2
A
A= (caller $frames + 2)[$elem]
Aresult
11
00
24 1 };
25  
26 # All fields required because we need
27 # to retain backwards compatibility.
28 1 my @sets = (
29 [qw(package pkg)],
30 [qw(filename file)],
31 'line',
32 [qw(subroutine sub)],
33 'hasargs',
34 'wantarray',
35 'evaltext',
36 'is_require',
37 'hints',
38 'bitmask'
39 );
40  
41 1 my %fields;
42 1 my $i = 0;
43 1 foreach my $set (@sets) {
44 10 ▶ foreach my $name (ref $set eq 'ARRAY' ? @$set : $set) {
Branch: 1 branch
BranchTrueFalseTotal
ref $set eq 'ARRAY' ? : 3 7 10
45 13 $fields{$name} = $i;
46 }
47 10 $i++;
48 }
49  
50 1 my $accessors = {};
51 1 foreach my $name (keys %fields) {
52 $accessors->{$name} = sub
53 {
54 14 ▶ my $frames = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
55 14 return $caller->($frames, $fields{$name});
56 13 };
57 }
58 1 $accessors->{_frames} = $frames;
59  
60 1 ▶ return bless $accessors, ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
61 }
62  
63 sub called_from_package
64 {
65 2 ▶ my $self = shift;
Subroutine called_from_package: called CC=3 SCAR=11.4
Pod documented
66 2 my ($called_from_package) = @_;
67 2 ▶ croak q(Usage: $caller->called_from_package('Package');)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $called_from_package 0 2 2
68 unless defined $called_from_package;
69  
70 2 ▶ return $self->{package}->() eq $called_from_package
Branch: 1 branch
BranchTrueFalseTotal
$self->{'package'}->() eq $called_from_package ? : 2 0 2
71 ? true : false;
72 }
73  
74 sub called_from_filename
75 {
76 2 ▶ my $self = shift;
Subroutine called_from_filename: called CC=3 SCAR=11.4
Pod documented
77 2 my ($called_from_filename) = @_;
78 2 ▶ croak q(Usage: $caller->called_from_filename('file');)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $called_from_filename 0 2 2
79 unless defined $called_from_filename;
80  
81 2 ▶ return $self->{filename}->() eq $called_from_filename
Branch: 1 branch
BranchTrueFalseTotal
$self->{'filename'}->() eq $called_from_filename ? : 2 0 2
82 ? true : false;
83 }
84  
85 sub called_from_line
86 {
87 1 ▶ my $self = shift;
Subroutine called_from_line: called CC=4 SCAR=15.6
Pod documented
88 1 my ($called_from_line) = @_;
89 1 ▶ croak q(Usage: $caller->called_from_line(42);)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $called_from_line and $called_from_line =~ /^\d+$/ 0 1 1
Condition33%1/3
defined $called_from_line and $called_from_line =~ /^\d+$/
!ll&&!rl&&r
001
MC/DC0%0/2
defined $called_from_line and $called_from_line =~ /^\d+$/
defined $called_from_line $called_from_line =~ /^\d+$/
Truth table33%1/3
A and B
A= defined $called_from_lineB= $called_from_line =~ /^\d+$/
ABresult
0X0
100
111
90 unless defined $called_from_line && $called_from_line =~ /^\d+$/;
91  
92 1 ▶ return $self->{line}->() == $called_from_line
Branch: 1 branch
BranchTrueFalseTotal
$self->{'line'}->() == $called_from_line ? : 1 0 1
93 ? true : false;
94 }
95  
96 sub called_from_subroutine
97 {
98 2 ▶ my $self = shift;
Subroutine called_from_subroutine: called CC=3 SCAR=11.4
Pod documented
99 2 my ($called_from_subroutine) = @_;
100 2 ▶ croak q(Usage: $caller->called_from_subroutine('Package::sub');)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $called_from_subroutine 0 2 2
101 unless defined $called_from_subroutine;
102  
103 2 ▶ return $self->{subroutine}->($self->{_frames} + 1) eq $called_from_subroutine
Branch: 1 branch
BranchTrueFalseTotal
$self->{'subroutine'}->($self->{'_frames'} + 1) eq $called_from_subroutine ? : 2 0 2
104 ? true : false;
105 }
106  
107 # backwards compatibility (deprecated)
108 2 *called_from_pkg = \&called_from_package;
109 2 *called_from_file = \&called_from_filename;
110 2 *called_from_sub = \&called_from_subroutine;
111  
112 2 1;
113 __END__
« Build ↑ Summary