blib/lib/XML/Spice.pm

filter: Statementstmt 38.1% 42 / 110 Branchbran 7.5% 3 / 40 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 58.8% 10 / 17 Podpod 0.0% 0 / 1 totaltotal 30.0% 55 / 183 CC 30 SCAR 45.2
CC
30
Cov
28%
_xml
210.0
_serialise
110.0
x
30.0
CRAP 91.8
↑ Summary
1 package XML::Spice;
2 2 $XML::Spice::VERSION = '0.04';
3 2 require 5.008;
4  
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our $PRETTY_PRINT = 0;
11  
12 sub import {
13 3 ▶ my ($pkg, @args) = @_;
Subroutine import: called CC=4 SCAR=13.9
14  
15 3 my $them = caller();
16  
17 3 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) { } 1 2 3
18 1 for my $arg (@args) {
19 3 ▶ croak "Invalid element name '$arg'" if $arg !~ m/^[A-Za-z]\w*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $arg =~ /^[A-Za-z]\w*$/ 0 3 3
20  
21 {
22 2 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶ *{$them."::".$arg} = sub { x($arg, @_) };
Subroutine __ANON__: not called CC=1 SCAR=0.4
24 }
25 }
26 }
27  
28 else {
29 2 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 *{$them."::x"} = \&x;
31 }
32 }
33  
34 sub x {
35 0 ▶ my ($tag, @args) = @_;
Subroutine x: not called CC=5 SCAR=34.0
Pod undocumented
36  
37 0 my $chunk = {
38 tag => $tag,
39 attrs => {},
40 };
41  
42 0 for my $arg (@args) {
43 0 ▶ if (ref $arg eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq 'HASH') { } 0 0 0
44 0 for my $key (keys %$arg) {
45 0 ▶ if (!defined $arg->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $arg->{$key}) { } 0 0 0
46 0 delete $chunk->{attrs}->{$key};
47 } else {
48 0 $chunk->{attrs}->{$key} = "".$arg->{$key};
49 }
50 }
51 }
52  
53 else {
54 0 push @{$chunk->{sub}}, $arg;
55 }
56 }
57  
58 0 return bless $chunk, "XML::Spice::Chunk";
59 }
60  
61  
62 package # hide from PAUSE
63 XML::Spice::Chunk;
64  
65 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 use overload
71 2 ▶ '""' => \&_xml;
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 2 my $TIDY_LOADED;
74  
75 sub _xml {
76 0 ▶ my ($chunk) = @_;
Subroutine _xml: not called CC=14 SCAR=53.5
77  
78 0 ▶ if ($PRETTY_PRINT && !defined $TIDY_LOADED) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::Spice::PRETTY_PRINT and not defined $TIDY_LOADED) 0 0 0
Condition0%0/3
$XML::Spice::PRETTY_PRINT and not defined $TIDY_LOADED
!ll&&!rl&&r
000
MC/DC0%0/2
$XML::Spice::PRETTY_PRINT and not defined $TIDY_LOADED
$XML::Spice::PRETTY_PRINT not defined $TIDY_LOADED
Truth table0%0/3
A and not(B)
A= $XML::Spice::PRETTY_PRINTB= not defined $TIDY_LOADED
ABresult
0X0
100
111
79 0 eval { require XML::Tidy::Tiny };
80 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
81 0 carp "Couldn't load XML::Tidy::Tiny: $@";
82 0 $TIDY_LOADED = 0;
83 }
84 else {
85 0 $TIDY_LOADED = 1;
86 }
87 }
88  
89 0 ▶ my $WANT_PRETTY = $PRETTY_PRINT && $TIDY_LOADED;
Condition0%0/3
$XML::Spice::PRETTY_PRINT && $TIDY_LOADED
!ll&&!rl&&r
000
MC/DC0%0/2
$XML::Spice::PRETTY_PRINT && $TIDY_LOADED
$XML::Spice::PRETTY_PRINT $TIDY_LOADED
Truth table0%0/3
A && B
A= $XML::Spice::PRETTY_PRINTB= $TIDY_LOADED
ABresult
0X0
100
111
90  
91 0 ▶ return $chunk->{cached} if exists $chunk->{cached} && !$WANT_PRETTY;
Branch: 1 branch
BranchTrueFalseTotal
if exists $chunk->{"cached"} and not $WANT_PRETTY 0 0 0
Condition0%0/3
exists $chunk->{"cached"} and not $WANT_PRETTY
!ll&&!rl&&r
000
MC/DC0%0/2
exists $chunk->{"cached"} and not $WANT_PRETTY
exists $chunk->{"cached"} not $WANT_PRETTY
Truth table0%0/3
A and not(B)
A= exists $chunk->{"cached"}B= not $WANT_PRETTY
ABresult
0X0
100
111
92  
93 sub _escape_attr {
94 0 ▶ my ($val) = @_;
Subroutine _escape_attr: not called CC=1 SCAR=6.9
95 0 $val =~ s/'/'/g;
96 0 return $val;
97 }
98  
99 sub _escape_cdata {
100 0 ▶ my ($val) = @_;
Subroutine _escape_cdata: not called CC=1 SCAR=6.9
101 0 $val =~ s/&/&/g;
102 0 $val =~ s/</&lt;/g;
103 0 $val =~ s/>/&gt;/g;
104 0 $val =~ s/"/&quot;/g;
105 0 $val =~ s/([^\x20-\x7E])/'&#' . ord($1) . ';'/ge;
106 0 return $val;
107 }
108  
109 sub _serialise {
110 0 ▶ my ($chunk, @things) = @_;
Subroutine _serialise: not called CC=10 SCAR=47.0
111  
112 0 my $xml = '';
113  
114 0 for my $thing (@things) {
115 0 ▶ next if ! defined $thing;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $thing 0 0 0
116  
117 0 ▶ if (ref $thing eq "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $thing eq "CODE") 0 0 0
118 0 $thing = &{$thing};
119  
120 0 ▶ if (ref $thing eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $thing eq "HASH") 0 0 0
121 0 for my $key (keys %$thing) {
122 0 ▶ if (!defined $thing->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $thing->{$key}) { } 0 0 0
123 0 delete $chunk->{attrs}->{$key};
124 } else {
125 0 $chunk->{attrs}->{$key} = "".$thing->{$key};
126 }
127 }
128 0 undef $thing;
129 }
130  
131 0 redo;
132 }
133  
134 0 ▶ if (ref $thing eq "ARRAY") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $thing eq 'ARRAY') { } 0 0 0
elsif (ref $thing eq 'XML::Spice::Chunk') { } 0 0 0
135 0 $xml .= $chunk->_serialise(@$thing);
136 }
137  
138 elsif (ref $thing eq "XML::Spice::Chunk") {
139 0 $xml .= $thing->_xml;
140 }
141  
142 else {
143 0 ▶ next if $thing eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $thing eq "" 0 0 0
144 0 $xml .= _escape_cdata($thing);
145 }
146 }
147  
148 0 return $xml;
149 }
150  
151 0 ▶ my $subxml = $chunk->_serialise(@{$chunk->{sub}}) if exists $chunk->{sub};
Branch: 1 branch
BranchTrueFalseTotal
if exists $chunk->{"sub"} 0 0 0
152  
153 0 my $xml = "<" . $chunk->{tag};
154  
155 0 for my $attr (keys %{$chunk->{attrs}}) {
156 0 $xml .= " $attr='" . _escape_attr($chunk->{attrs}->{$attr}) . "'";
157 }
158  
159 0 ▶ if (!defined $subxml) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $subxml) 0 0 0
160 0 $xml .= "/>";
161 0 ▶ $chunk->{cached} = $xml unless $WANT_PRETTY;
Branch: 1 branch
BranchTrueFalseTotal
unless $WANT_PRETTY 0 0 0
162 0 ▶ return $WANT_PRETTY ? XML::Tidy::Tiny::xml_tidy($xml) : $xml;
Branch: 1 branch
BranchTrueFalseTotal
$WANT_PRETTY ? : 0 0 0
163 }
164  
165 0 $xml .= ">" . $subxml . "</" . $chunk->{tag} . ">";
166  
167 0 ▶ $chunk->{cached} = $xml unless $WANT_PRETTY;
Branch: 1 branch
BranchTrueFalseTotal
unless $WANT_PRETTY 0 0 0
168 0 ▶ return $WANT_PRETTY ? XML::Tidy::Tiny::xml_tidy($xml) : $xml;
Branch: 1 branch
BranchTrueFalseTotal
$WANT_PRETTY ? : 0 0 0
169 }
170  
171 sub forget {
172 0 ▶ my ($chunk) = @_;
Subroutine forget: not called CC=1 SCAR=6.9
173  
174 0 delete $chunk->{cached};
175 }
176  
177 2 1;
178  
179 __END__
↑ Summary