blib/lib/Data/Dx.pm

filter: Statementstmt 61.7% 92 / 149 Branchbran 46.8% 15 / 32 Conditioncond 46.1% 12 / 26 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 69.2% 18 / 26 Podpod n/a totaltotal 56.7% 139 / 245 CC 30 SCAR 21.9
CC
30
Cov
57%
unimport
20.0
_format_data
18.8
import
10.0
CRAP 8.9
↑ Summary
1 package Data::Dx;
2  
3 =encoding utf8
4  
5 =cut
6  
7 5 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 our $VERSION = '0.000011';
12  
13 5 ▶use Keyword::Declare;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 5 my %COLOUR = (
16 key => 'bold ansi136', # Soft orange
17 value => 'cyan',
18 punct => 'bold ansi245', # Mid-grey
19 comment => 'blue',
20 );
21  
22 5 my @OUTPUT;
23 5 ▶BEGIN { @OUTPUT = \*STDERR }
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 42 ▶sub _dx {
Subroutine _dx: called CC=2 SCAR=6.9
26 42 my ($expr) = @_;
27 5 ▶ use List::Util 'max';
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 # Flatten the expression to a single line...
30 42 $expr =~ s{\s+}{ }g;
31  
32 # Simple arrays and hashes need to be dumped by reference...
33 42 ▶ my $ref = $expr =~ /^[\@%][\w:]++$/ ? q{\\} : q{};
Branch: 1 branch
BranchTrueFalseTotal
$expr =~ /^[\@%][\w:]++$/u ? : 6 36 42
34  
35 # How much to indent...
36 42 my $indent = ' ' x (length($expr) + 3);
37  
38 # Handle unbalanced {...} in the expression...
39 42 my $str_expr = $expr;
40 42 $str_expr =~ s{ ( [\\\{\}] ) }{\\$1}xmsg;
41  
42 # Generate the source...
43 42 return qq{Data::Dx::_format_data(__LINE__, __FILE__, q{$str_expr}, q{$indent}, $ref $expr);};
44 }
45  
46  
47 4 ▶sub import {
Subroutine import: called CC=8 SCAR=23.0
48 4 my (undef, $opt_ref) = @_;
49 4 ▶ my %opt = ( colour => -t *STDERR, %{$opt_ref//{}} );
Condition100%2/2
$opt_ref // {}
l!l
31
MC/DC100%1/1
$opt_ref // {}
$opt_ref
Truth table100%2/2
A
A= $opt_ref
Aresult
11
00
50  
51 # Lexical colour control...
52 $^H{'Data::Dx no_colour'} = 1
53 4 ▶ if !$opt{colour};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"colour"} 4 0 4
54  
55 # Lexical output redirect...
56 4 ▶ if ($opt{to}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"to"}) 3 1 4
57 3 $^H{'Data::Dx output'} = @OUTPUT;
58 3 push @OUTPUT, $opt{to};
59 }
60 4  
61 0 ▶ keyword Dx (Expr $expr) { _dx($expr) }
Subroutine __ANON__: called CC=1 SCAR=0.6
Subroutine __ANON__: not called CC=1 SCAR=0.6
Branch: 1 branch
BranchTrueFalseTotal
if (((caller 0)[3] // "") =~ /\b(?:un)?import\Z/u) 4 0 4
Condition50%1/2
(caller 0)[3] // ""
l!l
40
MC/DC0%0/1
(caller 0)[3] // ""
(caller 0)[3]
Truth table50%1/2
A
A= (caller 0)[3]
Aresult
11
00
62 0 ▶ keyword Dₓ (Expr $expr) { _dx($expr) }
Subroutine __ANON__: called CC=1 SCAR=0.5
Subroutine __ANON__: not called CC=1 SCAR=0.5
Branch: 1 branch
BranchTrueFalseTotal
if (((caller 0)[3] // "") =~ /\b(?:un)?import\Z/u) 4 0 4
Condition50%1/2
(caller 0)[3] // ""
l!l
40
MC/DC0%0/1
(caller 0)[3] // ""
(caller 0)[3]
Truth table50%1/2
A
A= (caller 0)[3]
Aresult
11
00
63 0  
64 0 utf8->import();
65 0 }
66 0 ▶ 
Subroutine BEGIN: called CC=1 SCAR=0.3
67 0 ▶sub unimport {
Subroutine BEGIN: called CC=1 SCAR=4.3
Subroutine unimport: not called CC=5 SCAR=29.9
68 0 ▶ keyword Dx (Expr $expr) { }
Subroutine __ANON__: not called CC=1 SCAR=6.2
Subroutine __ANON__: not called CC=1 SCAR=6.2
Branch: 1 branch
BranchTrueFalseTotal
if (((caller 0)[3] // "") =~ /\b(?:un)?import\Z/u) 0 0 0
Condition0%0/2
(caller 0)[3] // ""
l!l
00
MC/DC0%0/1
(caller 0)[3] // ""
(caller 0)[3]
Truth table0%0/2
A
A= (caller 0)[3]
Aresult
11
00
69 0 ▶ keyword Dₓ (Expr $expr) { }
Subroutine __ANON__: not called CC=1 SCAR=6.0
Subroutine __ANON__: not called CC=1 SCAR=6.0
Branch: 1 branch
BranchTrueFalseTotal
if (((caller 0)[3] // "") =~ /\b(?:un)?import\Z/u) 0 0 0
Condition0%0/2
(caller 0)[3] // ""
l!l
00
MC/DC0%0/1
(caller 0)[3] // ""
(caller 0)[3]
Truth table0%0/2
A
A= (caller 0)[3]
Aresult
11
00
70 0 }
71 0  
72 0 ▶sub _color {
Subroutine _color: called CC=3 SCAR=14.7
73 0 ▶ state $colorer = eval {
Subroutine BEGIN: called CC=1 SCAR=2.0
74 0 ▶ require Win32::Console::ANSI if $^O eq 'MSWin32';
Subroutine BEGIN: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 3 3
75 5 require Term::ANSIColor;
76 135 ▶ sub { return shift if ((caller 1)[10]//{})->{'Data::Dx no_colour'}
Subroutine __ANON__: called CC=5 SCAR=22.0
77 135 ▶ || ((caller 2)[10]//{})->{'Data::Dx no_colour'};
Branch: 1 branch
BranchTrueFalseTotal
if +((caller 1)[10] // {})->{"Data::Dx no_colour"} or +((caller 2)[10] // {})->{"Data::Dx no_colour"} 135 0 135
Condition42%3/7
+((caller 1)[10] // {})->{"Data::Dx no_colour"} or +((caller 2)[10] // {})->{"Data::Dx no_colour"}
l!l&&r!l&&!r
01350
(caller 1)[10] // {}
l!l
1350
(caller 2)[10] // {}
l!l
1350
MC/DC0%0/2
+((caller 1)[10] // {})->{"Data::Dx no_colour"} or +((caller 2)[10] // {})->{"Data::Dx no_colour"}
(caller 1)[10] (caller 2)[10]
Truth table0%0/3
A or B
A= (caller 1)[10]B= (caller 2)[10]
ABresult
1X1
011
000
78 0 goto &Term::ANSIColor::colored;
79 }
80 0 ▶ } // sub { shift };
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
eval { do { require Win32::Console::ANSI if $^O eq "MSWin32"; require Term::ANSIColor; sub { return shift() if +((caller 1)[10] // {})->{"Data::Dx no_colour"} or +((caller 2)[10] // {})->{"Data::Dx no_colour"}; goto \&Term::ANSIColor::colored; } } } // sub { shift(); }
l!l&&r!l&&!r
300
MC/DC0%0/2
eval { do { require Win32::Console::ANSI if $^O eq "MSWin32"; require Term::ANSIColor; sub { return shift() if +((caller 1)[10] // {})->{"Data::Dx no_colour"} or +((caller 2)[10] // {})->{"Data::Dx no_colour"}; goto \&Term::ANSIColor::colored; } } } // sub { shift(); }
eval { do { require Win32::Console::ANSI if $^O eq "MSWin32"; require Term::ANSIColor; sub { return shift() if +((caller 1)[10] // {})->{"Data::Dx no_colour"} or +((caller 2)[10] // {})->{"Data::Dx no_colour"}; goto \&Term::ANSIColor::colored; } } } sub { shift(); }
Truth table33%1/3
A // B
A= eval { do { require Win32::Console::ANSI if $^O eq "MSWin32"; require Term::ANSIColor; sub { return shift() if +((caller 1)[10] // {})->{"Data::Dx no_colour"} or +((caller 2)[10] // {})->{"Data::Dx no_colour"}; goto \&Term::ANSIColor::colored; } } }B= sub { shift(); }
ABresult
1X1
011
000
81 135 $colorer->(@_);
82 }
83  
84 42 ▶sub _format_data {
Subroutine _format_data: called CC=12 SCAR=29.3
85 # Unpack leadings args...
86 42 my $linenum = shift;
87 42 my $filename = shift;
88 42 my $expr = shift;
89 42 my $indent = shift;
90  
91 # Serialize any Contextual::Return::Value objects (which break dump())...
92 42 for my $arg (@_) {
93 51 ▶ if ((ref($arg)||q{}) =~ m{\A Contextual::Return}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $arg || "") =~ /\A Contextual::Return/msux) 0 51 51
Condition100%2/2
ref $arg || ''
l!l
942
MC/DC100%1/1
ref $arg || ''
ref $arg
Truth table100%2/2
A
A= ref $arg
Aresult
11
00
94 0 require Contextual::Return;
95 0 Contextual::Return::FREEZE($arg);
96 }
97 }
98  
99 # Then repack data...
100 42 ▶ my $data = @_ > 1 ? [@_] : shift;
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 6 36 42
101  
102 # Lexical configurations...
103 42 ▶ my $hint = ((caller 0)[10] // {});
Condition50%1/2
(caller 0)[10] // {}
l!l
420
MC/DC0%0/1
(caller 0)[10] // {}
(caller 0)[10]
Truth table50%1/2
A
A= (caller 0)[10]
Aresult
11
00
104  
105 # Dump the data...
106 42 my $dump;
107 42 ▶ if (!defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $data) { } 9 33 42
108 9 $dump = _color('undef', $COLOUR{value});
109 }
110 else {
111 5 ▶ use Data::Dump 'dump';
Subroutine BEGIN: called CC=1 SCAR=0.0
112 33 $dump = dump($data);
113  
114 33 ▶ if (!$hint->{'Data::Dx no_colour'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hint->{"Data::Dx no_colour"}) 0 33 33
115 0 my $bw_dump = $dump;
116 0 $dump = q{};
117 0 $bw_dump
118 =~ s{ $PPR::GRAMMAR
119 (?: (?<key> (?: (?&PerlString) | (?&PerlBareword) ) (?= \s*+ => ) )
120 | (?<literal> (?&PerlLiteral) | sub \s*+ { \s*+ ... \s*+ } )
121 | (?<punct> \S )
122 | (?<space> . )
123 )
124 }{
125 $dump .= exists $+{key} ? _color( "$+{key}", $COLOUR{key} )
126 : exists $+{punct} ? _color( "$+{punct}", $COLOUR{punct} )
127 : exists $+{literal} ? _color( "$+{literal}", $COLOUR{value} )
128 0 ▶ : "$+{space}";
Branch: 3 branches
BranchTrueFalseTotal
exists $+{'key'} ? : 0 0 0
exists $+{'punct'} ? : 0 0 0
exists $+{'literal'} ? : 0 0 0
129 0 "";
130 }gxmseo;
131 }
132  
133 33 $dump =~ s{ (?! \A ) ^ }{$indent}gxms;
134 }
135  
136 42 ▶ my $output = $OUTPUT[$hint->{'Data::Dx output'} // 0];
Condition50%1/2
$hint->{"Data::Dx output"} // 0
l!l
420
MC/DC0%0/1
$hint->{"Data::Dx output"} // 0
$hint->{"Data::Dx output"}
Truth table50%1/2
A
A= $hint->{"Data::Dx output"}
Aresult
11
00
137  
138 42 print {$output}
139 _color("#line $linenum $filename\n", $COLOUR{comment}),
140 _color($expr, $COLOUR{key}),
141 42 _color(' = ', $COLOUR{punct}),
142 "$dump\n\n";
143 }
144  
145  
146  
147  
148 5 1; # Magic true value required at end of module
149 __END__
↑ Summary