blib/lib/Data/Dump/SExpression.pm

filter: Statementstmt 27.5% 22 / 80 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 44.4% 4 / 9 Podpod 100% 2 / 2 totaltotal 19.5% 28 / 143 CC 26 SCAR 58.1
CC
26
Cov
17%
_dump
506.0
_dd_or_dump
30.0
_double_quote
2.0
CRAP 332.1
↑ Summary
1 package Data::Dump::SExpression;
2  
3 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4 1 our $DATE = '2020-04-10'; # DATE
5 1 our $DIST = 'Data-Dump-SExpression'; # DIST
6 1 our $VERSION = '0.001'; # VERSION
7  
8 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Scalar::Util qw(looks_like_number blessed reftype refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 require Exporter;
15 1 our @ISA = qw(Exporter);
16 1 our @EXPORT_OK = qw(dd_sexp dump_sexp);
17  
18 1 our %_seen_refaddrs;
19  
20 # BEGIN COPY PASTE FROM Data::Dump
21 1 my %esc = (
22 "\a" => "\\a",
23 "\b" => "\\b",
24 "\t" => "\\t",
25 "\n" => "\\n",
26 "\013" => "\\v", # chr(11)
27 "\f" => "\\f",
28 "\r" => "\\r",
29 "\e" => "\\e",
30 #"\040" => "\\e", # chr(32)
31 "\177" => "\\d", # chr(127)
32 "\\" => "\\\\",
33 '"' => "\\\"",
34 );
35  
36 0 ▶sub _double_quote {
Subroutine _double_quote: not called CC=1 SCAR=6.9
37 0 my $str = shift;
38  
39 0 $str =~ s/([\a\b\t\n\013\f\r\e\177\\"])/$esc{$1}/g;
40 0 return qq("$str");
41 }
42  
43 0 ▶sub _dump {
Subroutine _dump: not called CC=22 SCAR=62.3
44 0 my $val = shift;
45  
46 0 my $ref = ref($val);
47 0 ▶ if ($ref eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "") 0 0 0
48 0 ▶ if (!defined($val)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $val) { } 0 0 0
elsif (&looks_like_number($val)) { } 0 0 0
49 0 return "nil";
50 } elsif (looks_like_number($val)) {
51 0 ▶ if ($val == "Inf") {
Branch: 3 branches
BranchTrueFalseTotal
if ($val == 'Inf') { } 0 0 0
elsif ($val == '-Inf') { } 0 0 0
elsif ($val =~ /nan/i) { } 0 0 0
52 0 return "1.0e+INF";
53 } elsif ($val == "-Inf") {
54 0 return "-1.0e+INF";
55 } elsif ($val =~ /nan/i) {
56 0 return "1.0e+NaN";
57 } else {
58 0 return $val;
59 }
60 } else {
61 0 return _double_quote($val);
62 }
63 }
64 0 my $refaddr = refaddr($val);
65 0 ▶ if ($_seen_refaddrs{$refaddr}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Data::Dump::SExpression::_seen_refaddrs{$refaddr}++) 0 0 0
66 0 die "Cannot handle circular references";
67 }
68  
69 0 ▶ if ($ref eq 'Regexp' || $ref eq 'REGEXP') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "Regexp" or $ref eq "REGEXP") 0 0 0
Condition0%0/3
$ref eq "Regexp" or $ref eq "REGEXP"
l!l&&r!l&&!r
000
MC/DC0%0/2
$ref eq "Regexp" or $ref eq "REGEXP"
$ref eq "Regexp" $ref eq "REGEXP"
Truth table0%0/3
A or B
A= $ref eq "Regexp"B= $ref eq "REGEXP"
ABresult
1X1
011
000
70 0 die "Cannot dump regexp objects";
71 }
72  
73 0 ▶ if ($ref eq 'JSON::PP::Boolean') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref eq "JSON::PP::Boolean") 0 0 0
74 0 ▶ return $val ? 't' : 0; # XXX should we dump false as nil or 0?
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 0 0 0
75 }
76  
77 0 ▶ if (blessed $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($val)) 0 0 0
78 0 die "Cannot dump blessed references";
79 }
80  
81 0 my $res;
82 0 ▶ if ($ref eq 'ARRAY') {
Branch: 5 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 0 0 0
elsif ($ref eq 'HASH') { } 0 0 0
elsif ($ref eq 'SCALAR') { } 0 0 0
elsif ($ref eq 'REF') { } 0 0 0
elsif ($ref eq 'CODE') { } 0 0 0
83 0 $res = "(";
84 0 my $i = 0;
85 0 for (@$val) {
86 0 ▶ $res .= " " if $i;
Branch: 1 branch
BranchTrueFalseTotal
if $i 0 0 0
87 0 $res .= _dump($_);
88 0 $i++;
89 }
90 0 $res .= ")";
91 } elsif ($ref eq 'HASH') {
92 0 $res = "(";
93 0 my $i = 0;
94 0 for (sort keys %$val) {
95 0 ▶ $res .= " " if $i++;
Branch: 1 branch
BranchTrueFalseTotal
if $i++ 0 0 0
96 0 my $k = _double_quote($_);
97 0 my $v = _dump($val->{$_});
98 0 $res .= "($k . $v)";
99 }
100 0 $res .= ")";
101 } elsif ($ref eq 'SCALAR') {
102 0 die "Cannot dump scalarrefs";
103 } elsif ($ref eq 'REF') {
104 0 die "Cannot dump refrefs";
105 } elsif ($ref eq 'CODE') {
106 0 die "Cannot dump coderefs";
107 } else {
108 0 die "Cannot dump $val (ref=$ref)";
109 }
110  
111 0 $res;
112 }
113  
114 1 our $_is_dd;
115 0 ▶sub _dd_or_dump {
Subroutine _dd_or_dump: not called CC=5 SCAR=34.0
116 0 local %_seen_refaddrs;
117  
118 0 my $res;
119 0 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 0 0 0
120 0 $res = "(" . join(" ", map {_dump($_)} @_) . ")";
121 } else {
122 0 $res = _dump($_[0]);
123 }
124  
125 0 ▶ if ($_is_dd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Data::Dump::SExpression::_is_dd) { } 0 0 0
126 0 say $res;
127 0 ▶ return wantarray() || @_ > 1 ? @_ : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray || @_ > 1 ? : 0 0 0
Condition0%0/3
wantarray or @_ > 1
l!l&&r!l&&!r
000
MC/DC0%0/2
wantarray or @_ > 1
wantarray @_ > 1
Truth table0%0/3
A or B
A= wantarrayB= @_ > 1
ABresult
1X1
011
000
128 } else {
129 0 return $res;
130 }
131 }
132  
133 0 ▶sub dd_sexp { local $_is_dd=1; _dd_or_dump(@_) } # goto &sub doesn't work with local
Subroutine dd_sexp: not called CC=1 SCAR=6.9
Pod documented
134 0 ▶sub dump_sexp { goto &_dd_or_dump }
Subroutine dump_sexp: not called CC=1 SCAR=6.9
Pod documented
135  
136 1 1;
137 # ABSTRACT: Dump Perl data structures as S-expression
138  
139 __END__
↑ Summary