blib/lib/Scalar/Util/Reftype.pm

filter: Statementstmt 88.1% 112 / 127 Branchbran 87.5% 21 / 24 Conditioncond 83.3% 15 / 18 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 92.5% 25 / 27 Podpod n/a totaltotal 87.2% 178 / 204 CC 25 SCAR 17.2
CC
25
Cov
88%
__ANON__
12.0
_object
10.0
_analyze
10.0
CRAP 5.6
↑ Summary
1 package Scalar::Util::Reftype;
2 2 $Scalar::Util::Reftype::VERSION = '0.46';
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use constant RESET_COUNTER => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 # being kept for backwards compatibility, 5.10 and later have it
9 2 ▶use constant HAS_FORMAT_REF => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use constant PRIMITIVES => qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
11 ARRAY
12 CODE
13 FORMAT
14 GLOB
15 HASH
16 IO
17 LVALUE
18 REF
19 Regexp
20 SCALAR
21 2 );
22 2 ▶use subs qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
23 blessed
24 class
25 container
26 object
27 reftype
28 type
29 2 );
30 2 ▶use overload bool => '_bool',
Subroutine BEGIN: called CC=1 SCAR=0.0
31 fallback => 1,
32 2 ;
33 2 ▶use re ();
Subroutine BEGIN: called CC=1 SCAR=0.0
34 2 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 2 our @EXPORT = qw( reftype );
38 2 our @EXPORT_OK = qw( type HAS_FORMAT_REF );
39  
40 2 my $OID;
41 2 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
42 2 $OID = RESET_COUNTER;
43 2 foreach my $type ( PRIMITIVES ) {
44 20 constant->import( 'TYPE_' . $type, ++$OID );
45 20 constant->import( 'TYPE_' . $type . '_OBJECT', ++$OID );
46 }
47 }
48  
49 2 ▶use constant CONTAINER => ++$OID;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 2 ▶use constant BLESSED => ++$OID;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 2 ▶use constant OVERRIDE => ++$OID;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 2 ▶use constant MAXID => $OID;
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 2 ▶BEGIN {
Subroutine BEGIN: called CC=4 SCAR=16.7
55 2 *class = \*container;
56 2 *type = \*reftype;
57 2 *object = \*blessed;
58 2 my(@types, @obj_idx);
59 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
60 2 foreach my $sym ( keys %{ __PACKAGE__ . q{::} } ) {
61 92 ▶ if ( $sym =~ m{ \A TYPE_ (.+?) \z }xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sym =~ / \A TYPE_ (.+?) \z /msx) 40 52 92
62 40 push @types, $1;
63 40 push @obj_idx, $sym;
64 }
65 }
66  
67 2 foreach my $meth ( @types ) {
68 40 ▶ *{ lc $meth } = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
69 30 my $self = shift;
70 30 my $id = 'TYPE_' . $meth;
71 30 return $self->[ $self->$id() ];
72 }
73 40 }
74  
75 0 ▶ *_dump = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
76 0 my $self = shift;
77 0 my %type = map { $self->$_() => $_ } @obj_idx;
78 0 my %val = map { $type{$_} => $self->[$_] } 0..$#obj_idx;
79 0 my $max = ( sort { $b <=> $a } map { length $_ } keys %val)[0];
80 0 my $rm = 'TYPE_';
81 0 $max -= length $rm;
82 0 for my $name ( sort { lc $a cmp lc $b } keys %val) {
83 0 (my $display = $name) =~ s{ \A $rm }{}xms;
84 0 ▶ printf "% ${max}s: %s\n", $display, $val{ $name } ? 'true' : '';
Branch: 1 branch
BranchTrueFalseTotal
$val{$name} ? : 0 0 0
85 }
86 2 };
87 }
88  
89 34 ▶sub reftype {
Subroutine reftype: called CC=1 SCAR=0.0
90 34 my @args = @_;
91 34 my $o = __PACKAGE__->_new;
92 34 return $o->_analyze( @args )
93 }
94  
95 34 ▶sub _new {
Subroutine _new: called CC=1 SCAR=0.0
96 34 my $class = shift;
97 34 my $self = [ map { 0 } 0..MAXID ];
98 34 $self->[CONTAINER] = q{};
99 34 bless $self, $class;
100 34 return $self;
101 }
102  
103 34 ▶sub _analyze {
Subroutine _analyze: called CC=10 SCAR=23.0
104 34 my $self = shift;
105 34 ▶ my $thing = shift || return $self;
Condition100%2/2
shift() || (return $self)
l!l
313
MC/DC100%1/1
shift() || (return $self)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
106 31 ▶ my $ref = CORE::ref($thing) || return $self;
Condition100%2/2
CORE::ref $thing || (return $self)
l!l
292
MC/DC100%1/1
CORE::ref $thing || (return $self)
CORE::ref $thing
Truth table100%2/2
A
A= CORE::ref $thing
Aresult
11
00
107  
108 29 foreach my $type ( PRIMITIVES ) {
109 142 ▶ my $id = $ref eq $type ? sprintf( 'TYPE_%s', $type )
Branch: 2 branches
BranchTrueFalseTotal
$ref eq $type ? : 14 128 142
$self->_object($thing, $type) ? : 15 113 128
110 : $self->_object($thing, $type) ? sprintf( 'TYPE_%s_OBJECT', $type )
111 : undef
112 ;
113 142 ▶ if ( $id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id) 29 113 142
114 29 ▶ $self->[ $self->$id() ] = 1 if ! $self->[OVERRIDE];
Branch: 1 branch
BranchTrueFalseTotal
unless $self->[22] 24 5 29
115 # IO refs are always objects
116 29 ▶ $self->[TYPE_IO] = 1 if $id eq 'TYPE_IO_OBJECT';
Branch: 1 branch
BranchTrueFalseTotal
if $id eq "TYPE_IO_OBJECT" 0 29 29
117 29 ▶ $self->[CONTAINER] = $ref if $self->[BLESSED];
Branch: 1 branch
BranchTrueFalseTotal
if $self->[21] 15 14 29
118 29 last;
119 }
120 }
121  
122 29 return $self;
123 }
124  
125 3 ▶sub container { return shift->[CONTAINER] }
Subroutine container: called CC=1 SCAR=0.0
126 0 ▶sub blessed { return shift->[BLESSED] }
Subroutine blessed: not called CC=1 SCAR=6.9
127  
128 128 ▶sub _object {
Subroutine _object: called CC=10 SCAR=23.1
129 128 my($self, $object, $type)= @_;
130 128 ▶ my $blessed = Scalar::Util::blessed( $object ) || return;
Condition100%2/2
builtin::blessed($object) || (return)
l!l
7355
MC/DC100%1/1
builtin::blessed($object) || (return)
builtin::blessed($object)
Truth table100%2/2
A
A= builtin::blessed($object)
Aresult
11
00
131 73 my $rt = Scalar::Util::reftype( $object );
132  
133 # new perl (5.24+ ?) messes the detection
134 73 ▶ if ( $rt
Branch: 1 branch
BranchTrueFalseTotal
if ($rt and $blessed and $rt eq "REGEXP" || $rt eq "SCALAR" and $blessed eq "Regexp") 8 65 73
Condition75%9/12
$rt and $blessed and $rt eq "REGEXP" || $rt eq "SCALAR" and $blessed eq "Regexp"
!ll&&!rl&&r
34318
$rt and $blessed and $rt eq "REGEXP" || $rt eq "SCALAR"
!ll&&!rl&&r
03439
$rt and $blessed
!ll&&!rl&&r
0073
$rt eq "REGEXP" or $rt eq "SCALAR"
l!l&&r!l&&!r
93034
MC/DC40%2/5
$rt and $blessed and $rt eq "REGEXP" || $rt eq "SCALAR" and $blessed eq "Regexp"
$rt $blessed $rt eq "REGEXP" $rt eq "SCALAR" $blessed eq "Regexp"
Truth table57%4/7
A and B and C or D and E
A= $rtB= $blessedC= $rt eq "REGEXP"D= $rt eq "SCALAR"E= $blessed eq "Regexp"
ABCDEresult
0XXXX0
10XXX0
1100X0
111X00
110100
111X11
110111
135 && $blessed
136 # new 5.10
137 && ( $rt eq 'REGEXP' || $rt eq 'SCALAR')
138 && $blessed eq 'Regexp'
139 ) {
140 8 return;
141 }
142  
143 65 $self->[BLESSED] = 1;
144  
145 65 ▶ if ( $rt eq 'IO' ) { # special case: IO
Branch: 1 branch
BranchTrueFalseTotal
if ($rt eq "IO") 4 61 65
146 4 $self->[TYPE_IO_OBJECT] = 1;
147 4 $self->[TYPE_IO] = 1;
148 4 $self->[OVERRIDE] = 1;
149 4 return 1;
150 }
151  
152 61 ▶ if ( re::is_regexp( $object ) ) { # special case: Regexp
Branch: 1 branch
BranchTrueFalseTotal
if (&re::is_regexp($object)) 1 60 61
153 1 $self->[TYPE_Regexp_OBJECT] = 1;
154 1 $self->[OVERRIDE] = 1;
155 1 return 1;
156 }
157  
158 60 ▶ return if $rt ne $type; # || ! ( $blessed eq 'IO' && $blessed eq $type );
Branch: 1 branch
BranchTrueFalseTotal
if $rt ne $type 50 10 60
159 10 return 1;
160 }
161  
162 1 ▶sub _bool {
Subroutine _bool: called CC=1 SCAR=0.0
163 1 require Carp;
164 1 Carp::croak(
165 'reftype() objects can not be used in boolean contexts. '
166 .'Please call one of the test methods on the return value instead. '
167 .'Example: `print 42 if reftype( \$thing )->array;`'
168 );
169 }
170  
171 2 1;
172  
173 __END__
↑ Summary