| 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
| |||||||||||||||||||||||||||||||||||||
| 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 // {}
MC/DC100%1/1
$opt_ref // {}
$opt_ref
Truth table100%2/2
A
A= $opt_ref
| |||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||
| 51 | # Lexical colour control... | ||||||||||||||||||||||||||||||||||||
| 52 | $^H{'Data::Dx no_colour'} = 1 | ||||||||||||||||||||||||||||||||||||
| 53 | 4 | ▶ | if !$opt{colour}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||
| 55 | # Lexical output redirect... | ||||||||||||||||||||||||||||||||||||
| 56 | 4 | ▶ | if ($opt{to}) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 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
Condition50%1/2
(caller 0)[3] // ""
MC/DC0%0/1
(caller 0)[3] // ""
(caller 0)[3]
Truth table50%1/2
A
A= (caller 0)[3]
| |||||||||||||||||||||||||||||||||||||
| 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
Condition50%1/2
(caller 0)[3] // ""
MC/DC0%0/1
(caller 0)[3] // ""
(caller 0)[3]
Truth table50%1/2
A
A= (caller 0)[3]
| |||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/2
(caller 0)[3] // ""
MC/DC0%0/1
(caller 0)[3] // ""
(caller 0)[3]
Truth table0%0/2
A
A= (caller 0)[3]
| |||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/2
(caller 0)[3] // ""
MC/DC0%0/1
(caller 0)[3] // ""
(caller 0)[3]
Truth table0%0/2
A
A= (caller 0)[3]
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 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
Condition42%3/7
+((caller 1)[10] // {})->{"Data::Dx no_colour"} or +((caller 2)[10] // {})->{"Data::Dx no_colour"}
(caller 1)[10] // {}
(caller 2)[10] // {}
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]
| |||||||||||||||||||||||||||||||||||||
| 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();
}
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();
}
| |||||||||||||||||||||||||||||||||||||
| 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
Condition100%2/2
ref $arg || ''
MC/DC100%1/1
ref $arg || ''
ref $arg
Truth table100%2/2
A
A= ref $arg
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||
| 102 | # Lexical configurations... | ||||||||||||||||||||||||||||||||||||
| 103 | 42 | ▶ | my $hint = ((caller 0)[10] // {}); | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
(caller 0)[10] // {}
MC/DC0%0/1
(caller 0)[10] // {}
(caller 0)[10]
Truth table50%1/2
A
A= (caller 0)[10]
| |||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||
| 105 | # Dump the data... | ||||||||||||||||||||||||||||||||||||
| 106 | 42 | my $dump; | |||||||||||||||||||||||||||||||||||
| 107 | 42 | ▶ | if (!defined $data) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$hint->{"Data::Dx output"} // 0
$hint->{"Data::Dx output"}
Truth table50%1/2
A
A= $hint->{"Data::Dx output"}
| |||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||