DOM/Element.pm

filter: Statementstmt 94.5% 69 / 73 Branchbran 95.8% 23 / 24 Conditioncond 78.5% 11 / 14 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 89.4% 17 / 19 Podpod n/a totaltotal 90.5% 125 / 138 CC 19 SCAR 15.1
CC
19
Cov
93%
AUTOLOAD
10.0
_gettext
6.0
__ANON__
2.0
CRAP 4.5
↑ Summary DOM.pm »
1 package XML::Tiny::DOM::Element;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use vars qw($VERSION $AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 use overload
8 8 ▶ '""' => sub { return shift()->_gettext(); },
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine __ANON__: called CC=1 SCAR=0.0
9 6 ▶ 'eq' => sub { my $s = shift(); return $s->_compare('eq', @_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
10 1 ▶ 'ne' => sub { my $s = shift(); return $s->_compare('ne', @_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
11 4 ▶ 'lt' => sub { my $s = shift(); return $s->_compare('lt', @_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
12 0 ▶ 'le' => sub { my $s = shift(); return $s->_compare('le', @_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
13 4 ▶ 'gt' => sub { my $s = shift(); return $s->_compare('gt', @_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
14 0 ▶ 'ge' => sub { my $s = shift(); return $s->_compare('ge', @_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
15 10 ▶ 'cmp' => sub { my $s = shift(); return $s->_compare('cmp', @_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
16 139 ▶ 'bool' => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
17 3 ;
18  
19 3 $VERSION = '1.1';
20  
21 =head1 NAME
22  
23 XML::Tiny::DOM::Element - an object representing an XML element
24  
25 =head1 DESCRIPTION
26  
27 This represents a single E<lt>element /E<gt> in an XML document that
28 was parsed using XML::Tiny::DOM.
29  
30 =head1 SYNOPSIS
31  
32 use XML::Tiny::DOM;
33 my $document = XML::Tiny::DOM->new(...);
34 # $document is now an XML::Tiny::DOM::Element
35  
36 now, given a document like this:
37  
38 <rsnapshot>
39 <externalprograms>
40 <rsync binary='/usr/bin/rsync'>
41 <args>
42 <arg>-a</arg>
43 <arg>-q</arg>
44 </args>
45 </rsync>
46 </externalprograms>
47 <intervals>
48 <interval name='alpha' retain='6' />
49 <interval name='beta' retain='7' />
50 <interval name='gamma' retain='4' />
51 <interval name='delta' retain='3' />
52 </intervals>
53 </rsnapshot>
54  
55 you can do this:
56  
57 my $rsync = $document->externalprograms->rsync();
58 my $rsyncbinary = $rsync->binary();
59 my $allrsyncargs = [ $rsync->args->arg('*') ];
60 my $secondarg = $rsync->args->arg(1);
61 my $gamma = $document->intervals->interval('gamma');
62  
63 =head1 METHODS
64  
65 =head2 created by AUTOLOAD
66  
67 Most methods are created using AUTOLOAD. There's also a few utility and
68 private methods whose names start with an underscore. Consequently
69 your documents shouldn't contain any elements or attributes whose names
70 start with an underscore, or that are called DESTROY or AUTOLOAD, because
71 those are special to perl.
72  
73 When you call the ...->foo() method on an object, it first looks for
74 an XML attribute with that name. If there is one, its value is returned.
75  
76 If there's no such attribute, then it looks for a child element with that
77 name. If no parameter is given, it returns the first such element. If a
78 numeric parameter is given, it returns the Nth element (counting from 0).
79 If the parameter is '*' then all such elements are returned. Otherwise,
80 child elements of the appropriate type are searched and a list of those whose
81 ...->name() method returns something that matches the parameter is returned.
82  
83 =head2 _parent
84  
85 Returns the parent element of this element. It is an error to call this
86 on the root element.
87  
88 =head2 _root
89  
90 Return the root element.
91  
92 =cut
93  
94 98 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=10 SCAR=23.0
95 98 (my $nodename = $AUTOLOAD) =~ s/.*:://;
96 98 my $self = shift();
97 98 ▶ my $wanted = shift() || 0;
Condition100%2/2
shift() || 0
l!l
1484
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
98  
99 98 ▶ return if($nodename eq 'DESTROY');
Branch: 1 branch
BranchTrueFalseTotal
if $nodename eq "DESTROY" 0 98 98
100  
101 # attribs take precedence ...
102 return $self->{attrib}->{$nodename}
103 98 ▶ if(exists($self->{attrib}->{$nodename}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"attrib"}{$nodename} 15 83 98
104  
105 83 my @childnodes = ();
106 83 foreach my $childnode (@{$self->{content}}) {
107 340 ▶ if($childnode->{type} eq 'e' && $childnode->{name} eq $nodename) {
Branch: 1 branch
BranchTrueFalseTotal
if ($childnode->{"type"} eq "e" and $childnode->{"name"} eq $nodename) 131 209 340
Condition66%2/3
$childnode->{"type"} eq "e" and $childnode->{"name"} eq $nodename
!ll&&!rl&&r
0209131
MC/DC50%1/2
$childnode->{"type"} eq "e" and $childnode->{"name"} eq $nodename
$childnode->{"type"} eq "e" $childnode->{"name"} eq $nodename
Truth table66%2/3
A and B
A= $childnode->{"type"} eq "e"B= $childnode->{"name"} eq $nodename
ABresult
0X0
100
111
108 131 push @childnodes, __PACKAGE__->_new($childnode, _parent => $self);
109 }
110 }
111 83 ▶ if($wanted eq '*') {
Branch: 2 branches
BranchTrueFalseTotal
if ($wanted eq '*') { } 2 81 83
elsif ($wanted =~ /^\d+$/) { } 80 1 81
112 2 return @childnodes;
113 } elsif($wanted =~ /^\d+$/) {
114 80 ▶ return $childnodes[$wanted] if(exists($childnodes[$wanted]));
Branch: 1 branch
BranchTrueFalseTotal
if exists $childnodes[$wanted] 79 1 80
115 1 die("Can't get '$nodename' number $wanted from object ".ref($self)."\n")
116 ;
117 } else {
118 1 return (grep { $_->name() eq $wanted } @childnodes);
119 }
120 }
121  
122 133 ▶sub _new {
Subroutine _new: called CC=2 SCAR=6.9
123 133 my $class = shift;
124 133 my $document = shift;
125 133 my %params = @_;
126 133 ▶ if($params{_parent}) { $document->{_parent} = $params{_parent}; }
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"_parent"}) 131 2 133
127 133 bless $document, $class;
128 }
129  
130 9 ▶sub _parent {
Subroutine _parent: called CC=2 SCAR=6.9
131 9 my $self = shift;
132 9 ▶ return $self->{_parent} if($self->{_parent});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_parent"} 7 2 9
133 2 die("Can't get root element's parent\n");
134 }
135  
136 1 ▶sub _root {
Subroutine _root: called CC=2 SCAR=6.9
137 1 my $self = shift;
138 1 $@ = 0;
139 1 ▶ while(!$@) { eval { $self = $self->_parent() }; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 3 1 4
140 1 return $self;
141 }
142  
143 33 ▶sub _gettext {
Subroutine _gettext: called CC=6 SCAR=18.0
144 33 my $self = shift;
145 33 my $c = $self->{content};
146 33 ▶ if(
Branch: 2 branches
BranchTrueFalseTotal
if (ref $c eq 'ARRAY' and defined $c->[0]{'type'} and $c->[0]{'type'} eq 't') { } 30 3 33
elsif (ref $c eq 'ARRAY' and keys %{$$c[0];} == 0) { } 1 2 3
Condition83%5/6
ref $c eq "ARRAY" and defined $c->[0]{"type"} and $c->[0]{"type"} eq "t"
!ll&&!rl&&r
1230
ref $c eq "ARRAY" and defined $c->[0]{"type"}
!ll&&!rl&&r
0132
MC/DC66%2/3
ref $c eq "ARRAY" and defined $c->[0]{"type"} and $c->[0]{"type"} eq "t"
ref $c eq "ARRAY" defined $c->[0]{"type"} $c->[0]{"type"} eq "t"
Truth table75%3/4
A and B and C
A= ref $c eq "ARRAY"B= defined $c->[0]{"type"}C= $c->[0]{"type"} eq "t"
ABCresult
0XX0
10X0
1100
1111
147 ref($c) eq 'ARRAY' &&
148 defined($c->[0]->{type}) &&
149 $c->[0]->{type} eq 't' # it's a text node
150 ) {
151 30 (my $value = $c->[0]->{content}) =~ s/^\s+|\s+$//g;
152 30 return $value;
153 ▶ } elsif(
Condition66%2/3
ref $c eq "ARRAY" and keys %{$$c[0];} == 0
!ll&&!rl&&r
021
MC/DC50%1/2
ref $c eq "ARRAY" and keys %{$$c[0];} == 0
ref $c eq "ARRAY" keys %{$$c[0];} == 0
Truth table66%2/3
A and B
A= ref $c eq "ARRAY"B= keys %{$$c[0];} == 0
ABresult
0X0
100
111
154 ref($c) eq 'ARRAY' &&
155 3 (keys %{$c->[0]}) == 0 # empty node
156 ) { # empty element stringifies to ''
157 1 return '';
158 } else {
159 2 die("Can't stringify '".$self->{name}."' in ".ref($self)."\n");
160 }
161 }
162  
163 25 ▶sub _compare {
Subroutine _compare: called CC=2 SCAR=6.9
164 25 my($self, $op, $comparand, $reversed) = @_;
165 25 my $value = $self->_gettext();
166 25 ▶ ($value, $comparand) = ($comparand, $value) if($reversed);
Branch: 1 branch
BranchTrueFalseTotal
if $reversed 10 15 25
167 25 return eval "\$value $op \$comparand";
168 }
169  
170 =head1 BUGS and FEEDBACK
171  
172 I welcome feedback about my code, including constructive criticism.
173 Bug reports should be made using L<http://rt.cpan.org/> or by email,
174 and should include the smallest possible chunk of code, along with
175 any necessary XML data, which demonstrates the bug. Ideally, this
176 will be in the form of a file which I can drop in to the module's
177 test suite.
178  
179 =head1 SEE ALSO
180  
181 L<XML::Tiny::DOM>
182  
183 L<XML::Tiny>
184  
185 =head1 AUTHOR, COPYRIGHT and LICENCE
186  
187 David Cantrell E<lt>F<david@cantrell.org.uk>E<gt>
188  
189 Copyright 2009 David Cantrell E<lt>david@cantrell.org.ukE<gt>
190  
191 This software is free-as-in-speech software, and may be used,
192 distributed, and modified under the terms of either the GNU
193 General Public Licence version 2 or the Artistic Licence. It's
194 up to you which one you use. The full text of the licences can
195 be found in the files GPL2.txt and ARTISTIC.txt, respectively.
196  
197 =head1 CONSPIRACY
198  
199 This module is also free-as-in-mason software.
200  
201 =cut
202  
203 3 '<one>zero</one>';
↑ Summary DOM.pm »