lib/XML/Declare.pm

filter: Statementstmt 96.3% 132 / 137 Branchbran 80.4% 37 / 46 Conditioncond 69.2% 9 / 13 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 85.7% 24 / 28 Podpod 100% 2 / 2 totaltotal 89.5% 206 / 230 CC 29 SCAR 18.4
CC
29
Cov
91%
element
16.2
doc
5.0
BEGIN
4.8
CRAP 6.3
↑ Summary
1 # XML::LibXML > 1.90 overloads Element
2 4 ▶use XML::LibXML ();
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 {
5 package # hide
6 XML::LibXML::Node;
7 4 ▶ use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶ use overload ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶ BEGIN {
Subroutine BEGIN: called CC=4 SCAR=15.8
10 12 ▶ my $overloaded = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
11 12 my ($m) = @_;
12 12 overload::ov_method(overload::mycan(__PACKAGE__,'('.$m),__PACKAGE__);
13 4 };
14 0 ▶ overload->import( '""' => sub { $_[0]->toString() } ) unless $overloaded->('""');
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
unless &$overloaded("\"\"") 0 4 4
15 0 ▶ overload->import( 'bool' => sub { 1 } ) unless $overloaded->('bool');
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
unless &$overloaded("bool") 0 4 4
16 0 ▶ overload->import( '0+' => sub { Scalar::Util::refaddr($_[0]) } ) unless $overloaded->('0+');
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
unless &$overloaded("0+") 0 4 4
17 4 overload->import( fallback => 1 );
18 }
19 }
20 {
21 4 package # hide
22 XML::LibXML::Element;
23 4 ▶ use overload ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶ BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.8
25 8 ▶ my $overloaded = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
26 8 my ($m) = @_;
27 8 overload::ov_method(overload::mycan(__PACKAGE__,'('.$m),__PACKAGE__);
28 4 };
29 0 ▶ overload->import( '""' => sub { $_[0]->toString() } ) unless $overloaded->('""');
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
unless &$overloaded("\"\"") 0 4 4
30 4 ▶ overload->import( fallback => 1 ) if $overloaded->('bool');
Branch: 1 branch
BranchTrueFalseTotal
if &$overloaded("bool") 4 0 4
31
32 }
33 }
34 4 package XML::Declare;
35  
36 4 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 =head1 NAME
42  
43 XML::Declare - Create XML documents with declaration style
44  
45 =cut
46  
47 4 our $VERSION = '0.06';
48  
49 =head1 SYNOPSIS
50  
51 my $doc = doc {
52 element feed => sub {
53 attr xmlns => 'http://www.w3.org/2005/Atom';
54 comment "generated using XML::Declare v$XML::Declare::VERSION";
55 for (1..3) {
56 element entry => sub {
57 element title => 'Title', type => 'text';
58 element content => sub {
59 attr type => 'text';
60 cdata 'Desc';
61 };
62 element published => '123123-1231-123-123';
63 element author => sub {
64 element name => 'Mons';
65 }
66 };
67 }
68 };
69 } '1.0','utf-8';
70  
71 print $doc;
72  
73 doc { DEFINITIONS } < args to XML::LibXML::Document->new >
74  
75 Where DEFINITIONS are
76
77 element name => sub { DEFINITIONS }
78 or
79 element
80 name => 'TextContent',
81 attr => value,
82 attr1 => [qw(more values)];
83
84 attr name => values;
85
86 text $content;
87
88 cdata $content;
89
90 comment $content;
91  
92 =head1 EXPORT
93  
94 =head2 doc BLOCK [ $version, $charset ];
95  
96 Create L<XML::LibXML::Document>;
97  
98 =head2 element $name, sub { ... };
99  
100 Create L<XML::LibXML::Element> with name C<$name>; everything, called within C<sub { ... }> will be appended as children to this element
101  
102 =head2 element $name, ATTRS
103  
104 Create L<XML::LibXML::Element> with name C<$name> and set it's attributes. C<ATTRS> is a pairs of C<key => "value">
105  
106 =head2 attr $name, $value
107  
108 Create L<XML::LibXML::Attribute> with name C<$name> and value C<$value>
109  
110 =head2 text $content
111  
112 Create L<XML::LibXML::Text> node with content C<$content>
113  
114 =head2 cdata $content
115  
116 Create L<XML::LibXML::CDATASection> node with content C<$content>
117  
118 =head2 comment $content
119  
120 Create L<XML::LibXML::Comment> node with content C<$content>
121  
122 =cut
123  
124  
125 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
126 4 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
127  
128 2 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
129 2 my $caller = caller;
130 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
131 2 *{ $caller . '::doc' } = \&doc;
132 2 *{ $caller . '::element' } = \&element;
133 2 *{ $caller . '::attr' } = \&attr;
134 2 *{ $caller . '::text' } = \&text;
135 2 *{ $caller . '::cdata' } = \&cdata;
136 2 *{ $caller . '::comment' } = \&comment;
137 }
138  
139 {
140 4 our $is_doc;
141 4 our $element;
142 sub element ($;$@);
143 sub attr (@);
144 3 ▶ sub _attr(@) {
Subroutine _attr: called CC=2 SCAR=6.9
145 eval {
146 3 $element->setAttribute(@_);
147 2 1;
148 3 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $XML::Declare::element->setAttribute(@_); 1 } } or do { (my $e = $@) =~ s/ at \S+? line \d+\.\s*$//; croak($e) } 2 1 3
149 1 ( my $e = $@ ) =~ s{ at \S+? line \d+\.\s*$}{};
150 1 croak $e;
151 };
152 }
153 sub text ($);
154 10 ▶ sub _text ($) {
Subroutine _text: called CC=1 SCAR=0.0
155 10 $element->appendChild(XML::LibXML::Text->new(shift));
156 }
157 sub cdata ($);
158 2 ▶ sub _cdata ($) {
Subroutine _cdata: called CC=1 SCAR=0.0
159 2 $element->appendChild(XML::LibXML::CDATASection->new(shift));
160 }
161 sub comment ($);
162 5 ▶ sub _comment ($) {
Subroutine _comment: called CC=3 SCAR=11.0
163 5 local $_ = shift;
164 5 ▶ m{--}s and croak "'--' (double-hyphen) MUST NOT occur within comments";
Branch: 1 branch
BranchTrueFalseTotal
/--/s and croak("'--' (double-hyphen) MUST NOT occur within comments") 1 4 5
165 4 ▶ substr($_,-1,1) eq '-' and croak "comment MUST NOT end with a '-' (hyphen)";
Branch: 1 branch
BranchTrueFalseTotal
substr($_, -1, 1) eq "-" and croak("comment MUST NOT end with a '-' (hyphen)") 1 3 4
166 3 $element->appendChild(XML::LibXML::Comment->new($_));
167 }
168
169 18 ▶ sub element($;$@) {
Subroutine element: called CC=16 SCAR=27.8
Pod documented
170 18 my $name = shift;
171 18 ▶ defined $element or
Branch: 1 branch
BranchTrueFalseTotal
defined $XML::Declare::element or local *attr = \&XML::Declare::_attr and local *text = \&XML::Declare::_text and local *cdata = \&XML::Declare::_cdata and local *comment = \&XML::Declare::_comment 17 1 18
Condition55%5/9
defined $XML::Declare::element || (local *attr = \&XML::Declare::_attr) && (local *text = \&XML::Declare::_text) && (local *cdata = \&XML::Declare::_cdata) && (local *comment = \&XML::Declare::_comment)
l!l&&r!l&&!r
1710
local *attr = \&XML::Declare::_attr and local *text = \&XML::Declare::_text and local *cdata = \&XML::Declare::_cdata and local *comment = \&XML::Declare::_comment
!ll
01
local *attr = \&XML::Declare::_attr and local *text = \&XML::Declare::_text and local *cdata = \&XML::Declare::_cdata
!ll
01
local *attr = \&XML::Declare::_attr and local *text = \&XML::Declare::_text
!ll
01
MC/DC0%0/2
defined $XML::Declare::element || (local *attr = \&XML::Declare::_attr) && (local *text = \&XML::Declare::_text) && (local *cdata = \&XML::Declare::_cdata) && (local *comment = \&XML::Declare::_comment)
defined $XML::Declare::element local *attr = \&XML::Declare::_attr
Truth table66%2/3
A || B
A= defined $XML::Declare::elementB= local *attr = \&XML::Declare::_attr
ABresult
1X1
011
000
172 local *attr = \&_attr and
173 local *text = \&_text and
174 local *cdata = \&_cdata and
175 local *comment = \&_comment;
176 18 my ($code,$text);
177 18 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 16 2 18
178 16 ▶ if (ref $_[-1] eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[-1] eq 'CODE') { } 9 7 16
179 9 $code = pop;
180 } else {
181 7 $text = shift;
182 }
183 }
184 18 my $new;
185 {
186 #local $element = $doc->createElement($name);
187 18 local $element;
188 eval {
189 18 $new = XML::LibXML::Element->new($name);
190 18 $new->setNodeName($name); # Will invoke checks
191 17 1;
192 18 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $new = "XML::LibXML::Element"->new($name); $new->setNodeName($name); 1 } } or do { (my $e = $@) =~ s/ at \S+? line \d+\.\s*$//; croak($e) } 17 1 18
193 1 ( my $e = $@ ) =~ s{ at \S+? line \d+\.\s*$}{};
194 1 croak $e;
195 };
196 17 ▶ $new->appendText($text) if defined $text;
Branch: 1 branch
BranchTrueFalseTotal
if defined $text 7 10 17
197 17 ▶ while (my( $attr,$val ) = splice @_, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 3 17 20
198 3 ▶ $new->setAttribute($attr, ref $val eq 'ARRAY' ? @$val : $val);
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 0 3 3
199 }
200 17 ▶ if ($code) {{
Branch: 1 branch
BranchTrueFalseTotal
if ($code) 9 8 17
201 9 local $element = $new;
202 9 local $is_doc;
203 9 ▶ $code->() if $code;
Branch: 1 branch
BranchTrueFalseTotal
if $code 9 0 9
204 #$element->appendChild($_) for @EL;
205 }}
206 #push @EL,$element;
207 }
208 14 ▶ if (defined $is_doc) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $XML::Declare::is_doc) { } 11 3 14
elsif (defined $XML::Declare::element) { } 2 1 3
209 11 ▶ if ( $is_doc > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($XML::Declare::is_doc > 0) { } 1 10 11
210 1 $element->appendChild($new);
211 } else {
212 10 $element->setDocumentElement($new);
213 10 $is_doc++;
214 }
215 11 return;
216 } elsif (defined $element) {
217 2 $element->appendChild($new);
218 2 return;
219 } else {
220 1 return $new;
221 }
222
223 }
224
225 19 ▶ sub doc (&;$$) {
Subroutine doc: called CC=5 SCAR=16.1
Pod documented
226 19 my $code = shift;
227 19 ▶ my $version = shift || '1.0';
Condition100%2/2
shift() || '1.0'
l!l
217
MC/DC100%1/1
shift() || '1.0'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
228 19 ▶ my $encoding = shift || 'utf-8';
Condition100%2/2
shift() || 'utf-8'
l!l
217
MC/DC100%1/1
shift() || 'utf-8'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
229 19 my $doc = XML::LibXML::Document->new($version, $encoding);
230 19 my $oldwarn = $SIG{__WARN__};
231 12 ▶ local $SIG{__WARN__} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
232 12 my $warn = shift;
233 12 substr($warn, rindex($warn, ' at '),-1,'');
234 12 chomp $warn;
235 12 ▶ local $SIG{__WARN__} = $oldwarn if defined $oldwarn;
Branch: 1 branch
BranchTrueFalseTotal
if defined $oldwarn 12 0 12
236 12 Carp::carp $warn;
237 19 };
238 19 local $element = $doc;
239 4 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
240 19 local *attr = \&_attr;
241 19 local *text = \&_text;
242 19 local *cdata = \&_cdata;
243 19 local *comment = \&_comment;
244 19 local $is_doc = 0;
245 19 $code->();
246 15 ▶ if ($is_doc == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($XML::Declare::is_doc == 0) { } 5 10 15
elsif ($XML::Declare::is_doc > 1) { } 0 10 10
247 5 Carp::carp "Empty document";
248 }
249 elsif ($is_doc > 1) {
250 0 Carp::carp "More than one root element. All except first are ignored";
251 }
252 15 $doc;
253 }
254 }
255  
256  
257 =head1 AUTHOR
258  
259 Mons Anderson <mons@cpan.org>
260  
261 =head1 LICENSE AND COPYRIGHT
262  
263 Copyright 2009-2010 Mons Anderson.
264  
265 This program is free software; you can redistribute it and/or modify it
266 under the terms of either: the GNU General Public License as published
267 by the Free Software Foundation; or the Artistic License.
268  
269 =cut
270  
271 4 1; # End of XML::Declare
↑ Summary