blib/lib/Object/Util.pm

filter: Statementstmt 76.5% 206 / 269 Branchbran 62.5% 85 / 136 Conditioncond 59.3% 19 / 32 MC/DCmcdc 30.0% 6 / 20 Subroutinesub 87.1% 34 / 39 Podpod 0.0% 0 / 3 totaltotal 70.1% 350 / 499 CC 82 SCAR 23.6
CC
82
Cov
71%
_dwarn_call
42.0
_murder
24.1
_detect_metaclass
23.9
CRAP 10.6
↑ Summary
1 19 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 19 ▶BEGIN { if ($] < 5.010000) { require UNIVERSAL::DOES } };
Subroutine BEGIN: called CC=2 SCAR=9.2
Branch: 1 branch
BranchTrueFalseTotal
if ($] < "5.01") 0 19 19
6  
7 package Object::Util;
8  
9 19 our $AUTHORITY = 'cpan:TOBYINK';
10 19 our $VERSION = '0.009';
11  
12 19 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 19 ▶use List::Util 1.29 qw( pairkeys pairvalues );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 19 ▶use Scalar::Util 1.23 qw( blessed reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 19 my $anon_class_id = 0;
17  
18 {
19 19 my %op = (
20 HASH => '%{}',
21 ARRAY => '@{}',
22 SCALAR => '${}',
23 CODE => '&{}',
24 GLOB => '*{}',
25 Regexp => 'qr',
26 );
27
28 sub _is_reftype
29 {
30 312 ▶ my $object = shift;
Subroutine _is_reftype: called CC=5 SCAR=16.2
31 312 my $type = $_[0];
32
33 312 ▶ return !!1 if (reftype($object)||'') eq $type;
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($object) || "") eq $type 282 30 312
Condition100%2/2
builtin::reftype($object) || ''
l!l
28329
MC/DC100%1/1
builtin::reftype($object) || ''
builtin::reftype($object)
Truth table100%2/2
A
A= builtin::reftype($object)
Aresult
11
00
34 30 ▶ return !!0 if not $INC{'overload.pm'};
Branch: 1 branch
BranchTrueFalseTotal
unless $INC{"overload.pm"} 0 30 30
35
36 30 ▶ my $op = $op{$type} or return !!0;
Branch: 1 branch
BranchTrueFalseTotal
my $op = $op{$type} or 0 30 30
37 30 !!overload::Method($object, $op);
38 }
39 }
40  
41 19 sub _new :method
42 {
43 5 ▶ my $class = shift;
Subroutine _new: called CC=5 SCAR=16.2
44 5 my $ref = reftype($class);
45
46 5 ▶ if ($ref)
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) { } 2 3 5
47 {
48 2 ▶ croak "Invocant is not a coderef"
Branch: 1 branch
BranchTrueFalseTotal
unless _is_reftype($class, "CODE") 0 2 2
49 unless _is_reftype($class, 'CODE');
50 }
51 else
52 {
53 3 require Module::Runtime;
54 3 Module::Runtime::use_package_optimistically($class);
55 3 ▶ croak "Class $class does not provide a constructor called 'new'"
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can("new") 0 3 3
56 unless $class->can("new");
57 }
58
59 5 ▶ $ref ? $class->(@_) : $class->new(@_);
Branch: 1 branch
BranchTrueFalseTotal
$ref ? : 2 3 5
60 }
61  
62 sub _call_if_object :method
63 {
64 3 ▶ my $object = shift;
Subroutine _call_if_object: called CC=2 SCAR=6.9
65 3 my $method = shift;
66 3 ▶ return unless blessed($object);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($object) 1 2 3
67 2 $object->$method(@_);
68 }
69  
70 19 for my $method (qw/ isa does DOES /)
71 {
72 3 ▶ eval qq{
Subroutine _DOES: called CC=2 SCAR=6.9
Subroutine _does: called CC=2 SCAR=6.9
Subroutine _isa: called CC=2 SCAR=6.9
Branch: 4 branches
BranchTrueFalseTotal
unless builtin::blessed($_[0]) 1 2 3
unless builtin::blessed($_[0]) 1 1 2
unless builtin::blessed($_[0]) 1 2 3
eval "\n\t\tsub _$method :method\n\t\t{\n\t\t\treturn unless Scalar::Util::blessed(\$_[0]);\n\t\t\tshift->$method(\@_);\n\t\t}\n\t\t1;\n\t" or die "Internal problem: $@" 0 57 57
73 sub _${method} :method
74 {
75 return unless Scalar::Util::blessed(\$_[0]);
76 shift->${method}(\@_);
77 }
78 1;
79 } or die "Internal problem: $@";
80 }
81  
82 sub _can :method
83 {
84 7 ▶ my $self = shift;
Subroutine _can: called CC=3 SCAR=11.0
85 7 ▶ return unless Scalar::Util::blessed($self);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self) 1 6 7
86 6 ▶ return $_[0] if ref($_[0]) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "CODE" 1 5 6
87 5 $self->can(@_);
88 }
89  
90 sub _try :method
91 {
92 2 ▶ my $object = shift;
Subroutine _try: called CC=1 SCAR=0.0
93 2 my $method = shift;
94 2 eval { $object->$method(@_) };
95 }
96  
97 sub _tap :method
98 {
99 2 ▶ my $object = shift;
Subroutine _tap: called CC=1 SCAR=0.0
100 2 my $method = shift;
101 2 $object->$method(@_);
102 2 $object;
103 }
104  
105 19 my %toolage;
106  
107 sub _detect_metaclass
108 {
109 13 ▶ my $class = shift;
Subroutine _detect_metaclass: called CC=12 SCAR=31.7
110
111 13 ▶ if ($INC{"Moo.pm"})
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"Moo.pm"}) 4 9 13
112 {
113 4 ▶ return "Moo" if $Moo::MAKERS{$class}{is_class};
Branch: 1 branch
BranchTrueFalseTotal
if $Moo::MAKERS{$class}{"is_class"} 2 2 4
114 }
115
116 11 ▶ if ($INC{'Moose.pm'})
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"Moose.pm"}) 4 7 11
117 {
118 4 require Moose::Util;
119 4 ▶ return "Moose" if Moose::Util::find_meta($class);
Branch: 1 branch
BranchTrueFalseTotal
if Moose::Util::find_meta($class) 4 0 4
120 }
121
122 7 ▶ if ($INC{'Mouse.pm'})
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"Mouse.pm"}) 0 7 7
123 {
124 0 require Mouse::Util;
125 0 ▶ return "Mouse" if Mouse::Util::find_meta($class);
Branch: 1 branch
BranchTrueFalseTotal
if Mouse::Util::find_meta($class) 0 0 0
126 }
127
128 7 my $meta;
129 7 ▶ eval { $meta = $class->meta } or return "Other";
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $meta = $class->meta } } or return "Other" 5 2 7
130
131 2 ▶ return "Moo" if ref($meta) eq "Moo::HandleMoose::FakeMetaClass";
Branch: 1 branch
BranchTrueFalseTotal
if ref $meta eq "Moo::HandleMoose::FakeMetaClass" 2 0 2
132 0 ▶ return "Mouse" if $meta->isa("Mouse::Meta::Module");
Branch: 1 branch
BranchTrueFalseTotal
if $meta->isa("Mouse::Meta::Module") 0 0 0
133 0 ▶ return "Moose" if $meta->isa("Moose::Meta::Class");
Branch: 1 branch
BranchTrueFalseTotal
if $meta->isa("Moose::Meta::Class") 0 0 0
134 0 ▶ return "Moose" if $meta->isa("Moose::Meta::Role");
Branch: 1 branch
BranchTrueFalseTotal
if $meta->isa("Moose::Meta::Role") 0 0 0
135 0 return "Other";
136 }
137  
138 sub _clone :method
139 {
140 8 ▶ my $object = shift;
Subroutine _clone: called CC=9 SCAR=22.4
141 8 my $class = blessed($object);
142
143 8 ▶ croak "Cannot call \$_clone on non-object"
Branch: 1 branch
BranchTrueFalseTotal
unless $class 1 7 8
144 unless $class;
145
146 7 ▶ return $object->clone(@_)
Branch: 1 branch
BranchTrueFalseTotal
if $object->can("clone") 1 6 7
147 if $object->can("clone");
148
149 6 ▶ my $tool = ($toolage{$class} ||= _detect_metaclass($class));
Condition66%2/3
$toolage{$class} ||= _detect_metaclass($class)
l!l&&r!l&&!r
240
MC/DC0%0/2
$toolage{$class} ||= _detect_metaclass($class)
$toolage{$class} _detect_metaclass($class)
Truth table66%2/3
A ||= B
A= $toolage{$class}B= _detect_metaclass($class)
ABresult
1X1
011
000
150
151 6 ▶ my %args = (@_ == 1 and ref($_[0]) eq "HASH") ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] eq 'HASH' ? : 1 5 6
Condition66%2/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
501
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
152
153 6 ▶ if ($tool eq "Moose")
Branch: 1 branch
BranchTrueFalseTotal
if ($tool eq "Moose") 2 4 6
154 {
155 2 require Moose::Util;
156 2 my $meta = Moose::Util::find_meta($class);
157 2 return $meta->clone_object($object, %args);
158 }
159
160 4 ▶ if ($tool eq "Mouse")
Branch: 1 branch
BranchTrueFalseTotal
if ($tool eq "Mouse") 0 4 4
161 {
162 0 require Mouse::Util;
163 0 my $meta = Mouse::Util::find_meta($class);
164 0 return $meta->clone_object($object, %args);
165 }
166
167 4 ▶ croak "Object does not provide a 'clone' method, and is not a hashref"
Branch: 1 branch
BranchTrueFalseTotal
unless _is_reftype($object, "HASH") 1 3 4
168 unless _is_reftype($object, 'HASH');
169
170 3 ref($object)->Object::Util::_new({ %$object, %args });
171 }
172  
173 sub _with_traits :method
174 {
175 11 ▶ my $class = shift;
Subroutine _with_traits: called CC=8 SCAR=21.7
176
177 11 ▶ if (ref $class)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) 2 9 11
178 {
179 2 ▶ croak "Cannot call \$_with_roles on reference"
Branch: 1 branch
BranchTrueFalseTotal
unless _is_reftype($class, "CODE") 0 2 2
180 unless _is_reftype($class, 'CODE');
181
182 2 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 0 2
183 {
184 2 my $factory = $class;
185 2 my @trait_list = @_;
186
187 return sub {
188 2 ▶ my $instance = $factory->(@_);
Subroutine __ANON__: called CC=1 SCAR=0.0
189 2 _extend($instance, [@trait_list]);
190 2 };
191 }
192 }
193
194 9 ▶ return $class unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 4 5 9
195
196 5 ▶ my $tool = ($toolage{$class} ||= _detect_metaclass($class));
Condition66%2/3
$toolage{$class} ||= _detect_metaclass($class)
l!l&&r!l&&!r
320
MC/DC0%0/2
$toolage{$class} ||= _detect_metaclass($class)
$toolage{$class} _detect_metaclass($class)
Truth table66%2/3
A ||= B
A= $toolage{$class}B= _detect_metaclass($class)
ABresult
1X1
011
000
197
198 5 ▶ if ($tool eq 'Moose')
Branch: 1 branch
BranchTrueFalseTotal
if ($tool eq "Moose") 1 4 5
199 {
200 1 require Moose::Util;
201 1 require MooX::Traits::Util;
202
203 1 my @traits = MooX::Traits::Util::resolve_traits($class, @_);
204 1 return Moose::Util::with_traits($class, @traits);
205 }
206
207 4 ▶ if ($tool eq 'Mouse')
Branch: 1 branch
BranchTrueFalseTotal
if ($tool eq "Mouse") 0 4 4
208 {
209 0 require Mouse::Util;
210 0 require MooX::Traits::Util;
211
212 0 my @traits = MooX::Traits::Util::resolve_traits($class, @_);
213
214 0 my $meta = ref(Mouse::Util::find_meta($class))->create(
215 sprintf('%s::__ANON__::%s', __PACKAGE__, ++$anon_class_id),
216 superclasses => [ $class ],
217 roles => \@traits,
218 cache => 1,
219 );
220 0 return $meta->name;
221 }
222
223 4 require Role::Tiny;
224 4 require MooX::Traits::Util;
225 4 MooX::Traits::Util::new_class_with_traits($class, @_);
226 }
227  
228 sub _dump :method
229 {
230 2 ▶ require Data::Dumper;
Subroutine _dump: called CC=2 SCAR=6.9
231 2 local $Data::Dumper::Terse = 1;
232 2 local $Data::Dumper::Indent = 1;
233 2 local $Data::Dumper::Useqq = 1;
234 2 local $Data::Dumper::Deparse = 1;
235 2 local $Data::Dumper::Quotekeys = 0;
236 2 local $Data::Dumper::Sortkeys = 1;
237 2 local $Data::Dumper::Trailingcomma = 1;
238
239 2 ▶ if ( _can($_[0], "dump") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_can($_[0], "dump")) 1 1 2
240 1 return shift->dump(@_);
241 }
242
243 1 Data::Dumper::Dumper($_[0]);
244 }
245  
246 sub _dwarn :method
247 {
248 0 ▶ require Data::Dumper;
Subroutine _dwarn: not called CC=2 SCAR=17.9
249 0 local $Data::Dumper::Terse = 1;
250 0 local $Data::Dumper::Indent = 1;
251 0 local $Data::Dumper::Useqq = 1;
252 0 local $Data::Dumper::Deparse = 1;
253 0 local $Data::Dumper::Quotekeys = 0;
254 0 local $Data::Dumper::Sortkeys = 1;
255 0 local $Data::Dumper::Trailingcomma = 1;
256
257 0 warn Data::Dumper::Dumper(@_);
258
259 0 ▶ wantarray ? @_ : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
260 }
261  
262 sub _dwarn_call :method
263 {
264 0 ▶ require Data::Dumper;
Subroutine _dwarn_call: not called CC=6 SCAR=37.4
265 0 local $Data::Dumper::Terse = 1;
266 0 local $Data::Dumper::Indent = 1;
267 0 local $Data::Dumper::Useqq = 1;
268 0 local $Data::Dumper::Deparse = 1;
269 0 local $Data::Dumper::Quotekeys = 0;
270 0 local $Data::Dumper::Sortkeys = 1;
271 0 local $Data::Dumper::Trailingcomma = 1;
272
273 0 my $object = shift;
274 0 my $method = shift;
275 0 my @args = @_;
276
277 0 warn "== INVOCANT ==\n";
278 0 warn Data::Dumper::Dumper($object);
279  
280 0 warn "== METHOD ==\n";
281 0 warn Data::Dumper::Dumper($method);
282  
283 0 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 0 0 0
284 0 warn "== ARGUMENTS ==\n";
285 0 warn Data::Dumper::Dumper(@args);
286 }
287
288 0 my @r;
289 0 ▶ if (wantarray) { @r = $object->$method(@args) }
Branch: 2 branches
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
elsif (defined wantarray) { } 0 0 0
290 0 elsif (defined wantarray) { @r = scalar $object->$method(@args) }
291 0 else { $object->$method(@args); undef; }
292
293 0 ▶ if (defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) 0 0 0
294 0 warn "== RETURN ==\n";
295 0 warn Data::Dumper::Dumper(@r);
296 }
297
298 0 ▶ wantarray ? @r : $r[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
299 }
300  
301 {
302 19 my %_cache;
303 sub _eigenclass
304 {
305 14 ▶ my $class = shift;
Subroutine _eigenclass: called CC=8 SCAR=20.9
306 14 my ($roles, $methods) = @_;
307
308 14 my @traits;
309 14 ▶ if ($roles and @$roles)
Branch: 1 branch
BranchTrueFalseTotal
if ($roles and @$roles) 5 9 14
Condition66%2/3
$roles and @$roles
!ll&&!rl&&r
095
MC/DC50%1/2
$roles and @$roles
$roles @$roles
Truth table66%2/3
A and B
A= $rolesB= @$roles
ABresult
0X0
100
111
310 {
311 5 require MooX::Traits::Util;
312 5 @traits = MooX::Traits::Util::resolve_traits($class, @$roles);
313 }
314
315 my $key = do
316 14 {
317 19 ▶ no warnings qw(once);
Subroutine BEGIN: called CC=1 SCAR=0.0
318 14 require Storable;
319 14 local $Storable::Deparse = 1;
320 14 local $Storable::canonical = 1;
321 14 Storable::freeze( [$class, \@traits, $methods] );
322 };
323
324 14 my $eigenclass = $_cache{$key};
325 14 ▶ unless ($eigenclass)
Branch: 1 branch
BranchTrueFalseTotal
unless ($eigenclass) 10 4 14
326 {
327 19 ▶ no strict qw(refs);
Subroutine BEGIN: called CC=1 SCAR=0.0
328 10 $_cache{$key}
329 = $eigenclass
330 = sprintf('%s::__ANON__::%s', __PACKAGE__, ++$anon_class_id);
331
332 10 ▶ my $tool = ($toolage{$class} ||= _detect_metaclass($class));
Condition66%2/3
$toolage{$class} ||= _detect_metaclass($class)
l!l&&r!l&&!r
370
MC/DC0%0/2
$toolage{$class} ||= _detect_metaclass($class)
$toolage{$class} _detect_metaclass($class)
Truth table66%2/3
A ||= B
A= $toolage{$class}B= _detect_metaclass($class)
ABresult
1X1
011
000
333 10 ▶ if ($tool eq "Moose")
Branch: 2 branches
BranchTrueFalseTotal
if ($tool eq 'Moose') { } 3 7 10
elsif ($tool eq 'Mouse') { } 0 7 7
334 {
335 3 require Moose::Meta::Class;
336 3 'Moose::Meta::Class'->create(
337 $eigenclass => (
338 superclasses => [$class],
339 (roles => \@traits) x!!@traits,
340 methods => $methods,
341 ),
342 );
343 }
344 elsif ($tool eq "Mouse")
345 {
346 0 require Mouse::Meta::Class;
347 0 'Mouse::Meta::Class'->create(
348 $eigenclass => (
349 superclasses => [$class],
350 (roles => \@traits) x!!@traits,
351 methods => $methods,
352 ),
353 );
354 }
355 else
356 {
357 7 *{"$eigenclass\::ISA"} = [ _with_traits($class, @traits) ];
358 7 *{"$eigenclass\::$_"} = $methods->{$_} for keys %$methods;
359 }
360 }
361
362 14 $eigenclass;
363 }
364 }
365  
366 19 sub _extend :method
367 {
368 17 ▶ my $object = shift;
Subroutine _extend: called CC=6 SCAR=18.0
369 17 ▶ my $class = blessed($object)
Branch: 1 branch
BranchTrueFalseTotal
my $class = builtin::blessed($object) or croak("Cannot call \$_extend on non-object") 0 17 17
370 or croak("Cannot call \$_extend on non-object");
371
372 17 ▶ my $roles = _is_reftype($_[0], "ARRAY") ? shift : [];
Branch: 1 branch
BranchTrueFalseTotal
_is_reftype($_[0], 'ARRAY') ? : 5 12 17
373 17 ▶ my $methods = _is_reftype($_[0], "HASH") ? shift : {@_};
Branch: 1 branch
BranchTrueFalseTotal
_is_reftype($_[0], 'HASH') ? : 0 17 17
374
375 17 ▶ return $object unless @$roles || keys(%$methods);
Branch: 1 branch
BranchTrueFalseTotal
unless @$roles or keys %$methods 3 14 17
Condition100%3/3
@$roles or keys %$methods
l!l&&r!l&&!r
593
MC/DC100%2/2
@$roles or keys %$methods
@$roles keys %$methods
Truth table100%3/3
A or B
A= @$rolesB= keys %$methods
ABresult
1X1
011
000
376 14 bless $object, _eigenclass($class, $roles, $methods);
377 }
378  
379 sub _murder :method
380 {
381 1 ▶ blessed($_[0]) or croak ("Cannot kill non-object");
Subroutine _murder: called CC=11 SCAR=31.8
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) or croak("Cannot kill non-object") 0 1 1
382
383 1 ▶ unless (exists &__REAPED__::AUTOLOAD) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists &__REAPED__::AUTOLOAD) 1 0 1
384 eval qq{
385 package __REAPED__;
386 sub $_ {
387 Object::Util::croak("Can't call method \\"$_\\" on reaped object");
388 }
389 1;
390 1 ▶ }||die($@) for qw(can isa DOES VERSION);
Subroutine DOES: not called CC=1 SCAR=1.7
Subroutine VERSION: not called CC=1 SCAR=1.7
Subroutine can: called CC=1 SCAR=1.7
Subroutine isa: not called CC=1 SCAR=1.7
Branch: 1 branch
BranchTrueFalseTotal
eval qq[\n\t\t\tpackage __REAPED__;\n\t\t\tsub $_ {\n\t\t\t\tObject::Util::croak("Can't call method \\"$_\\" on reaped object");\n\t\t\t}\n\t\t\t1;\n\t\t] or die $@ 0 4 4
391 1 ▶ eval q{
Subroutine AUTOLOAD: called CC=1 SCAR=0.0
Subroutine DESTROY: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
eval "\n\t\t\tpackage __REAPED__;\n\t\t\tsub AUTOLOAD {\n\t\t\t\t(my \$method = \$__REAPED__::AUTOLOAD) =~ s/.*:://;\n\t\t\t\tObject::Util::croak(\"Can't call method \\\"\$method\\\" on reaped object\");\n\t\t\t}\n\t\t\tsub DESTROY { 1; }\n\t\t\t1;\n\t\t" or die $@ 0 1 1
392 package __REAPED__;
393 sub AUTOLOAD {
394 (my $method = $__REAPED__::AUTOLOAD) =~ s/.*:://;
395 Object::Util::croak("Can't call method \"$method\" on reaped object");
396 }
397 sub DESTROY { 1; }
398 1;
399 }||die($@);
400 };
401
402 1 my $destroy = $_[0]->Object::Util::_can('DESTROY');
403 1 ▶ $_[0]->$destroy if $destroy;
Branch: 1 branch
BranchTrueFalseTotal
if $destroy 1 0 1
404  
405 1 my $reftype = reftype($_[0]);
406 1 ▶ if ($reftype eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if ($reftype eq 'HASH') { } 1 0 1
elsif ($reftype eq 'ARRAY') { } 0 0 0
elsif ($reftype eq 'SCALAR' or $reftype eq 'REF') { } 0 0 0
407 1 %{$_[0]} = ();
408 }
409 elsif ($reftype eq 'ARRAY') {
410 0 @{$_[0]} = ();
411 }
412 ▶ elsif ($reftype eq 'SCALAR' or $reftype eq 'REF') {
Condition0%0/3
$reftype eq "SCALAR" or $reftype eq "REF"
l!l&&r!l&&!r
000
MC/DC0%0/2
$reftype eq "SCALAR" or $reftype eq "REF"
$reftype eq "SCALAR" $reftype eq "REF"
Truth table0%0/3
A or B
A= $reftype eq "SCALAR"B= $reftype eq "REF"
ABresult
1X1
011
000
413 0 undef ${$_[0]};
414 }
415
416 1 bless $_[0], '__REAPED__';
417 1 undef $_[0];
418 }
419  
420 sub subs :method
421 {
422 55 ▶ '$_new' => \&_new,
Subroutine subs: called CC=1 SCAR=0.0
Pod undocumented
423 '$_isa' => \&_isa,
424 '$_can' => \&_can,
425 '$_does' => \&_does,
426 '$_DOES' => \&_DOES,
427 '$_call_if_object' => \&_call_if_object,
428 '$_tap' => \&_tap,
429 '$_try' => \&_try,
430 '$_clone' => \&_clone,
431 '$_with_traits' => \&_with_traits,
432 '$_dump' => \&_dump,
433 '$_dwarn' => \&_dwarn,
434 '$_dwarn_call' => \&_dwarn_call,
435 '$_extend' => \&_extend,
436 '$_murder' => \&_murder,
437 }
438  
439 sub sub_names :method
440 {
441 36 ▶ my $me = shift;
Subroutine sub_names: called CC=1 SCAR=0.0
Pod undocumented
442 36 pairkeys($me->subs);
443 }
444  
445 sub setup_for :method
446 {
447 18 ▶ my $me = shift;
Subroutine setup_for: called CC=5 SCAR=16.3
Pod undocumented
448 18 my @refs = @_;
449 18 my @subs = pairvalues($me->subs);
450
451 18 ▶ while (@refs)
Branch: 1 branch
BranchTrueFalseTotal
if (@refs) 270 18 288
452 {
453 270 my $ref = shift(@refs);
454 270 my $sub = shift(@subs);
455 270 ▶ die "Internal problem" unless _is_reftype($sub, 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
unless _is_reftype($sub, "CODE") 0 270 270
456
457 270 $$ref = $sub;
458 270 ▶ &Internals::SvREADONLY($ref, 1) if exists(&Internals::SvREADONLY);
Branch: 1 branch
BranchTrueFalseTotal
if exists &Internals::SvREADONLY 270 0 270
459 }
460
461 18 ▶ die "Internal problem" if @subs;
Branch: 1 branch
BranchTrueFalseTotal
if @subs 0 18 18
462 18 return;
463 }
464  
465 sub import :method
466 {
467 19 ▶ my $me = shift;
Subroutine import: called CC=8 SCAR=21.6
468 19 my (%args) = @_;
469 19 my ($caller, $file) = caller;
470
471 19 ▶ $args{magic} = "auto" unless defined $args{magic};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"magic"} 19 0 19
472
473 19 ▶ if ($file ne '-e'
Branch: 1 branch
BranchTrueFalseTotal
if ($file ne "-e" and $args{"magic"} and eval { do { require B::Hooks::Parser } }) 18 1 19
Condition50%3/6
$file ne "-e" and $args{"magic"} and eval {
!ll&&!rl&&r
0118
$file ne "-e" and $args{"magic"}
!ll&&!rl&&r
0019
MC/DC33%1/3
$file ne "-e" and $args{"magic"} and eval { do { require B::Hooks::Parser } }
$file ne "-e" $args{"magic"} eval { do { require B::Hooks::Parser } }
Truth table50%2/4
A and B and C
A= $file ne "-e"B= $args{"magic"}C= eval { do { require B::Hooks::Parser } }
ABCresult
0XX0
10X0
1100
1111
474 and $args{magic}
475 19 and eval { require B::Hooks::Parser })
476 {
477 18 my $varlist = join ',', $me->sub_names;
478 18 my $reflist = join ',', map "\\$_", $me->sub_names;
479 18 B::Hooks::Parser::inject(";my($varlist);$me\->setup_for($reflist);");
480 18 return;
481 }
482
483 1 ▶ if ($args{magic} and $args{magic} ne "auto")
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"magic"} and $args{"magic"} ne "auto") 0 1 1
Condition33%1/3
$args{"magic"} and $args{"magic"} ne "auto"
!ll&&!rl&&r
010
MC/DC0%0/2
$args{"magic"} and $args{"magic"} ne "auto"
$args{"magic"} $args{"magic"} ne "auto"
Truth table33%1/3
A and B
A= $args{"magic"}B= $args{"magic"} ne "auto"
ABresult
0X0
100
111
484 {
485 0 carp "Object::Util could not use magic; continuing regardless";
486 }
487
488 1 my %subs = $me->subs;
489 1 for my $sub_name (sort keys %subs)
490 {
491 15 my $code = $subs{$sub_name};
492 15 $sub_name =~ s/^.//;
493 19 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
494 15 *{"$caller\::$sub_name"} = \$code;
495 }
496 }
497  
498 19 1;
499  
500 __END__
501  
502 =pod
503  
504 =encoding utf-8
505  
506 =for stopwords metaobject
507  
508 =head1 NAME
509  
510 Object::Util - a selection of utility methods that can be called on blessed objects
511  
512 =head1 SYNOPSIS
513  
514 use Object::Util;
515
516 # $foo might be undef, but this should not die
517 if ($foo->$_isa("Bar")) {
518 ...;
519 }
520  
521 =head1 DESCRIPTION
522  
523 This module is inspired by L<Safe::Isa>, L<Object::Tap>, and my own
524 OO experiences. It is a hopefully helpful set of methods for working
525 with objects, exposed as lexical coderef variables.
526  
527 =head2 Rationale
528  
529 Providing methods as coderefs so that you can do:
530  
531 $object->$_foo(@args)
532  
533 ... is unusual, so probably requires some explanation.
534  
535 Firstly some of these methods are designed to be called on either a
536 blessed object or some kind of unblessed reference or value. Calling a
537 method on an unblessed reference like this will croak:
538  
539 $ref->foo(@args)
540  
541 Ditto calling methods on undef. Coderefs don't suffer from that
542 problem.
543  
544 More importantly though, the aim of this module is that these methods
545 should be available for you to call on I<any> object. You can only
546 call C<< $object->foo(@args) >> if C<< $object >>'s class implements a
547 method called C<foo>, or inherits from a superclass that does. Coderef
548 methods can be called on any object.
549  
550 This module adopts the C<< $_foo >> naming convention pioneered by
551 modules such as Safe::Isa. However (unlike Safe::Isa) the coderefs it
552 provides are I<< true lexical variables >> (a.k.a. C<my> variables),
553 not package variables (a.k.a. C<our> variables).
554  
555 =head2 Methods
556  
557 =head3 Object construction and manipulation
558  
559 =over
560  
561 =item C<< $_new >>
562  
563 Can be used like C<< $class->$_new(@args) >> to create a new object.
564 Object::Util will use L<Module::Runtime> to I<< attempt >> to load
565 C<< $class >> if it is not already loaded. C<< $class >> is expected
566 to provide a method called C<new>.
567  
568 Can also be used as C<< $factory->$_new(@args) >> to create a new
569 object, where C<< $factory >> is a coderef or an object overloading
570 C<< &{} >>. In this case, C<< $_new >> will simply call
571 C<< $factory->(@args) >> and expect that to return an object.
572  
573 =item C<< $_clone >>
574  
575 If the object provides a C<clone> method, calls that. Or if the object
576 appears to be Moose- or Mouse-based, clones it using the metaobject
577 protocol.
578  
579 Otherwise takes the naive approach of treating the object as a hashref
580 of attribute values, and creates a new object of the same class.
581  
582 # clone overrides some attributes from the original object
583 my $glenda = $glen->$_clone(name => "Glenda", gender => "f");
584  
585 That final fallback obviously massively breaks your class'
586 encapsulation, so it should be used sparingly.
587  
588 =item C<< $_with_traits >>
589  
590 Calling C<< $class->$_with_traits(@traits) >> will return a new class
591 name that does some extra traits. Should roughly support L<Moose>,
592 L<Mouse>, L<Moo>, and L<Role::Tiny>, though combinations of frameworks
593 (e.g. consuming a Moose role in a Mouse class) will not always work.
594  
595 If C<< $class >> is actually a (factory) coderef, then this will only
596 I<partly> work. Example:
597  
598 my $factory = sub { Foo->new(@_) };
599 my $instance = $factory->$_with_traits("Bar")->$_new(%args);
600  
601 The object C<< $instance >> should now be a C<Foo> object, and should
602 do the C<Bar> role, however if C<Bar> defines any I<attributes>, then
603 C<< $_new >> will not have initialized them correctly. This is because
604 of the opacity of the C<< $factory >>: C<< $_with_traits >> cannot
605 peek inside it and apply traits to the C<Foo> class; instead it needs
606 to build C<< $instance >> and apply the traits to the already-built
607 object. Therefore any behaviour that C<Bar> defines for the constructor
608 will have been ignored.
609  
610 It is sometimes possible to work around this issue using:
611  
612 my $factory = sub { Foo->new(@_) };
613 my $instance = $factory->$_with_traits("Bar")->$_new(%args);
614 $instance = $instance->$_clone(%args);
615  
616 =item C<< $_extend >>
617  
618 Calling C<< $object->$_extend(\@traits, \%methods) >> will add some
619 extra roles and/or methods to an existing object.
620  
621 Either C<< @traits >> or C<< %methods >> may be omitted. That is,
622 C<< $object->$_extend(\@traits) >> will add some traits to an existing
623 object but no new methods, and C<< $object->$_extend(\%methods) >>
624 will add new methods, but no traits. C<< $object->$_extend() >> also
625 works fine, and is a no-op.
626  
627 This method always returns C<< $object >>, which makes it suitable for
628 chaining.
629  
630 Like L<Object::Extend>, but with added support for roles.
631  
632 =item C<< $_murder >>
633  
634 Called on an object, tries its best to destroy the object.
635  
636 my $object = Foo->new;
637 $object->$_murder;
638 defined($object); # false
639  
640 Not only will C<< $object >> be set to undef in the above example, but
641 if there were any other references to the object, C<< $_murder >> will
642 do its best to poison them too.
643  
644 C<< $_murder >> will empty out the contents of the underlying hash
645 or array in hashref-based and arrayref-based objects, and for
646 scalarref-based objects will set the underlying scalar to undef.
647  
648 C<< $_murder >> will also rebless the reference into a dummy class where
649 all method calls will croak.
650  
651 C<< $_murder >> will generously ensure that the object's C<DESTROY> is
652 called before initiating the killing spree, allowing the class to perform
653 any necessary clean-up first.
654  
655 =back
656  
657 =head3 Method call modifiers
658  
659 =over
660  
661 =item C<< $_call_if_object >>
662  
663 C<< $object->$_call_if_object($method => @args) >> works like
664 C<< $object->$method(@args) >>, but if C<< $object >> is undefined,
665 returns C<undef> instead of throwing an exception.
666  
667 C<< $method >> may be a method name, or a coderef (anonymous method).
668  
669 Same as L<Safe::Isa>.
670  
671 =item C<< $_try >>
672  
673 C<< $object->$_try($method => @args) >> works like
674 C<< $object->$method(@args) >>, but if I<< any exception is thrown >>
675 returns C<undef> instead.
676  
677 C<< $method >> may be a method name, or a coderef (anonymous method).
678  
679 =item C<< $_tap >>
680  
681 C<< $object->$_tap($method => @args) >> works like
682 C<< $object->$method(@args) >>, but discards the method's return value
683 (indeed it calls the method in void context), and instead returns the
684 object itself, making it useful for chaining.
685  
686 C<< $method >> may be a method name, or a coderef (anonymous method).
687  
688 Same as L<Object::Tap>, or the C<tap> method in Ruby.
689  
690 =item C<< $_isa >>
691  
692 C<< $object->$_isa($class) >> works like C<isa> as defined in
693 L<UNIVERSAL>, but if C<< $object >> is undefined, returns false
694 instead of throwing an exception.
695  
696 A shortcut for C<< $object->$_call_if_object(isa => $class) >>.
697  
698 Same as L<Safe::Isa>.
699  
700 =item C<< $_does >>
701  
702 C<< $object->$_does($role) >> works like C<does> as defined in
703 L<Moose::Object>, but if C<< $object >> is undefined, returns false
704 instead of throwing an exception.
705  
706 A shortcut for C<< $object->$_call_if_object(does => $role) >>.
707  
708 Same as L<Safe::Isa>.
709  
710 =item C<< $_DOES >>
711  
712 C<< $object->$_DOES($role) >> works like C<DOES> as defined in
713 L<UNIVERSAL>, but if C<< $object >> is undefined, returns false
714 instead of throwing an exception.
715  
716 A shortcut for C<< $object->$_call_if_object(DOES => $role) >>.
717  
718 Same as L<Safe::Isa>.
719  
720 =item C<< $_can >>
721  
722 C<< $object->$_can($method) >> works like C<can> as defined in
723 L<UNIVERSAL>, but if C<< $object >> is undefined, returns C<undef>
724 instead of throwing an exception.
725  
726 There is one other significant deviation from the behaviour of
727 UNIVERSAL's C<can> method: C<< $_can >> also returns true if
728 C<$method> is an unblessed coderef. (The behaviour of C<$method> if it
729 is a blessed object -- particularly in the face of overloading -- can
730 be unintuitive, so is not supported by C<< $_can >>.)
731  
732 Similar to L<Safe::Isa>, but not quite the same.
733  
734 =back
735  
736 =head3 Object utility methods
737  
738 =over
739  
740 =item C<< $_dump >>
741  
742 Calling C<< $object->$_dump >> returns a L<Data::Dumper> dump of the
743 object, with some useful changes to the default Data::Dumper output.
744 (Same as L<Data::Dumper::Concise>.)
745  
746 If the object provides its own C<dump> method, this will be called
747 instead. Any additional arguments will be passed through to it.
748  
749 =item C<< $_dwarn >>
750  
751 Calling C<< $object->$_dwarn(@args) >> prints a similar dump of the
752 object and any arguments as a warning, then returns the object, so
753 is suitable for tap-like chaining.
754  
755 Unlike C<< $_dump >>, will not call the object's own C<dump> method.
756  
757 =item C<< $_dwarn_call >>
758  
759 Calling C<< $object->$_dwarn_call($method, @args) >> calls the
760 method on the object, passing it the arguments, and returns the
761 result. Along the way, it will dump the object, method, arguments,
762 and return value as warnings. Returns the method's return value.
763  
764 Unlike C<< $_dump >>, will not call the object's own C<dump> method.
765  
766 =back
767  
768 =head2 Implementation Details
769  
770 L<B::Hooks::Parser> is used to inject these methods into your
771 lexical scope, and C<Internals::SvREADONLY> (an internal function
772 built into the Perl core) is used to make them read-only, so you
773 can't do:
774  
775 use Object::Util;
776 $_isa = sub { "something else" };
777  
778 If this module detects that B::Hooks::Parser cannot be used on your
779 version of Perl, or your Perl is too old to have Internals::SvREADONLY,
780 then it has various fallback routes, but the variables it provides may
781 end up as package (C<our>) variables, or not be read-only.
782  
783 If the magic works on your version of Perl, but you wish to avoid the
784 magic anyway, you can switch it off:
785  
786 use Object::Util magic => 0;
787  
788 The magic may trigger bugs if you're loading Object::Util in a sufficiently
789 unusual way (for example, C<require> and C<import> instead of C<use>). If
790 this happens, disable magic.
791  
792 =head1 BUGS
793  
794 Please report any bugs to
795 L<http://rt.cpan.org/Dist/Display.html?Queue=Object-Util>.
796  
797 =head1 SEE ALSO
798  
799 L<Safe::Isa>, L<UNIVERSAL>, L<Object::Tap>, L<MooseX::Clone>,
800 L<Data::Dumper::Concise>, L<Object::Extend>.
801  
802 =head1 AUTHOR
803  
804 Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
805  
806 =head1 COPYRIGHT AND LICENCE
807  
808 This software is copyright (c) 2014, 2018 by Toby Inkster.
809  
810 This is free software; you can redistribute it and/or modify it under
811 the same terms as the Perl 5 programming language system itself.
812  
813 =head1 DISCLAIMER OF WARRANTIES
814  
815 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
816 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
817 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
818  
↑ Summary