blib/lib/UNIVERSAL/dump.pm

filter: Statementstmt 100% 40 / 40 Branchbran 100% 18 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 100% 64 / 64 CC 12 SCAR 17.8
CC
12
Cov
100%
import
9.0
__ANON__
4.0
BEGIN
1.0
CRAP 5.9
↑ Summary
1 package UNIVERSAL::dump;
2  
3 # version info
4 4 $VERSION= '0.11';
5  
6 # be as strict and verbose as possible
7 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # presets
11 4 my %preset= (
12 blessed => 'Scalar::Util::blessed',
13 dump => 'Data::Dumper::Dumper',
14 peek => 'Devel::Peek::Dump',
15 refaddr => 'Scalar::Util::refaddr',
16 );
17  
18 # installed handlers
19 4 my %installed;
20  
21 # satisfy require
22 4 1;
23  
24 #-------------------------------------------------------------------------------
25 #
26 # Perl specific subroutines
27 #
28 #-------------------------------------------------------------------------------
29 # IN: 1 class (ignored)
30 # 2..N method => subroutine pairs
31  
32 21 ▶sub import {
Subroutine import: called CC=9 SCAR=22.0
33 21 my $class= shift;
34  
35 # make sure default is set
36 21 ▶ unshift( @_,'dump' ) unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 20 21
37  
38 # allow for redefining subroutines
39 4 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 # handle all simple specifications
42 METHOD:
43 21 foreach my $spec (@_) {
44 27 ▶ if ( !ref($spec) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $spec) 10 17 27
45 die qq{Don't know how to install method "UNIVERSAL::$spec"\n}
46 10 ▶ unless my $sub= $preset{$spec};
Branch: 1 branch
BranchTrueFalseTotal
unless my $sub = $preset{$spec} 1 9 10
47 9 $class->import( { $spec => $sub } );
48 9 next METHOD;
49 }
50  
51 # all methods
52 17 foreach my $method ( keys %{$spec} ) {
53 17 my $sub= $spec->{$method};
54 17 ▶ $sub= $preset{$sub} if $preset{$sub};
Branch: 1 branch
BranchTrueFalseTotal
if $preset{$sub} 2 15 17
55  
56 # already installed
57 17 ▶ if ( my $installed= $installed{$method} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $installed = $installed{$method}) 4 13 17
58 4 ▶ die qq{Cannot install "UNIVERSAL::$method" with "$sub":}
Branch: 1 branch
BranchTrueFalseTotal
if $sub ne $installed 2 2 4
59 . qq{ already installed with "$installed"\n}
60 if $sub ne $installed;
61 }
62  
63 # mark method as installed
64 15 ( my $module= $sub ) =~ s#::[^:]+$##;
65 15 $module =~ s#::#/#;
66 15 $module .= ".pm";
67 15 $installed{$method}= $sub;
68  
69 # install the method in the indicated namespace
70 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
71 15 ▶ *{"UNIVERSAL::$method"}= sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
72 8 my $self = shift;
73 8 eval { require $module };
74 8 ▶ return $sub->( @_ ? @_ : $self ) if defined wantarray;
Branch: 2 branches
BranchTrueFalseTotal
if defined wantarray 6 2 8
@_ ? : 3 3 6
75 2 ▶ print STDERR $sub->( @_ ? @_ : $self );
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 1 2
76 } #UNIVERSAL::$method
77 15 }
78 }
79 } #import
80  
81 #---------------------------------------------------------------------------
82  
83 __END__
↑ Summary