RDF/Trine/Node/Literal.pm

filter: Statementstmt 72.2% 227 / 314 Branchbran 52.4% 107 / 204 Conditioncond 54.7% 23 / 42 MC/DCmcdc 44.0% 11 / 25 Subroutinesub 94.5% 35 / 37 Podpod 100% 18 / 18 totaltotal 65.7% 421 / 640 CC 118 SCAR 37.5
CC
118
Cov
64%
is_valid_lexical_form
120.9
canonicalize_literal_value
77.4
numeric_value
56.0
CRAP 42.4
« RDF/Trine/Store/DBI/Pg.pm ↑ Summary RDF/Trine/VariableBindings.pm »
1 # RDF::Trine::Node::Literal
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Trine::Node::Literal - RDF Node class for literals
7  
8 =head1 VERSION
9  
10 This document describes RDF::Trine::Node::Literal version 1.019
11  
12 =cut
13  
14 package RDF::Trine::Node::Literal;
15  
16 68 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 68 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 68 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 68 ▶use base qw(RDF::Trine::Node);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 68 ▶use RDF::Trine::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 68 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 68 ▶use Scalar::Util qw(blessed looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 68 ▶use Carp qw(carp croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 ######################################################################
27  
28 68 our ($VERSION, $USE_XMLLITERALS, $USE_FORMULAE);
29 68 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=12.7
30 68 $VERSION = '1.019';
31 0 ▶ eval "use RDF::Trine::Node::Literal::XML;"; ## no critic (ProhibitStringyEval)
Subroutine BEGIN: called CC=1 SCAR=1.2
32 68 ▶ $USE_XMLLITERALS = (RDF::Trine::Node::Literal::XML->can('new')) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
'RDF::Trine::Node::Literal::XML'->can('new') ? : 0 68 68
33 0 ▶ eval "use RDF::Trine::Node::Formula;"; ## no critic (ProhibitStringyEval)
Subroutine BEGIN: called CC=1 SCAR=1.2
34 68 ▶ $USE_FORMULAE = (RDF::Trine::Node::Formula->can('new')) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
'RDF::Trine::Node::Formula'->can('new') ? : 0 68 68
35 }
36  
37 ######################################################################
38  
39 1314 ▶use overload '""' => sub { $_[0]->sse },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
40 68 ;
41  
42 =head1 METHODS
43  
44 Beyond the methods documented below, this class inherits methods from the
45 L<RDF::Trine::Node> class.
46  
47 =over 4
48  
49 =cut
50  
51 =item C<new ( $string, $lang, $datatype, $canonical_flag )>
52  
53 Returns a new Literal structure.
54  
55 =cut
56  
57 2360 ▶sub new {
Subroutine new: called CC=12 SCAR=29.0
Pod documented
58 2360 my $class = shift;
59 2360 my $literal = shift;
60 2360 my $lang = shift;
61 2360 my $dt = shift;
62 2360 my $canon = shift;
63
64 2360 ▶ unless (defined($literal)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $literal) 0 2360 2360
65 0 throw RDF::Trine::Error::MethodInvocationError -text => "Literal constructor called with an undefined value";
66 }
67
68 2360 ▶ if (blessed($dt) and $dt->isa('RDF::Trine::Node::Resource')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($dt) and $dt->isa("RDF::Trine::Node::Resource")) 167 2193 2360
Condition66%2/3
builtin::blessed($dt) and $dt->isa("RDF::Trine::Node::Resource")
!ll&&!rl&&r
21930167
MC/DC50%1/2
builtin::blessed($dt) and $dt->isa("RDF::Trine::Node::Resource")
builtin::blessed($dt) $dt->isa("RDF::Trine::Node::Resource")
Truth table66%2/3
A and B
A= builtin::blessed($dt)B= $dt->isa("RDF::Trine::Node::Resource")
ABresult
0X0
100
111
69 167 $dt = $dt->uri_value;
70 }
71
72 2360 ▶ if ($dt and $canon) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dt and $canon) 25 2335 2360
Condition100%3/3
$dt and $canon
!ll&&!rl&&r
198734825
MC/DC100%2/2
$dt and $canon
$dt $canon
Truth table100%3/3
A and B
A= $dtB= $canon
ABresult
0X0
100
111
73 25 $literal = $class->canonicalize_literal_value( $literal, $dt );
74 }
75
76 2360 ▶ if ($USE_XMLLITERALS and defined($dt) and $dt eq 'http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral') {
Branch: 2 branches
BranchTrueFalseTotal
if ($RDF::Trine::Node::Literal::USE_XMLLITERALS and defined $dt and $dt eq 'http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral') { } 0 2360 2360
elsif ($RDF::Trine::Node::Literal::USE_FORMULAE and defined $dt and $dt eq 'RDF::Trine::Node::Formula'->literal_datatype) { } 0 2360 2360
Condition33%2/6
$RDF::Trine::Node::Literal::USE_XMLLITERALS and defined $dt and $dt eq "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral"
!ll&&!rl&&r
236000
$RDF::Trine::Node::Literal::USE_XMLLITERALS and defined $dt
!ll&&!rl&&r
236000
MC/DC0%0/3
$RDF::Trine::Node::Literal::USE_XMLLITERALS and defined $dt and $dt eq "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral"
$RDF::Trine::Node::Literal::USE_XMLLITERALS defined $dt $dt eq "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral"
Truth table25%1/4
A and B and C
A= $RDF::Trine::Node::Literal::USE_XMLLITERALSB= defined $dtC= $dt eq "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral"
ABCresult
0XX0
10X0
1100
1111
77 0 return RDF::Trine::Node::Literal::XML->new( $literal, $lang, $dt );
78 ▶ } elsif ($USE_FORMULAE and defined($dt) and $dt eq RDF::Trine::Node::Formula->literal_datatype) {
Condition33%2/6
$RDF::Trine::Node::Literal::USE_FORMULAE and defined $dt and $dt eq "RDF::Trine::Node::Formula"->literal_datatype
!ll&&!rl&&r
236000
$RDF::Trine::Node::Literal::USE_FORMULAE and defined $dt
!ll&&!rl&&r
236000
MC/DC0%0/3
$RDF::Trine::Node::Literal::USE_FORMULAE and defined $dt and $dt eq "RDF::Trine::Node::Formula"->literal_datatype
$RDF::Trine::Node::Literal::USE_FORMULAE defined $dt $dt eq "RDF::Trine::Node::Formula"->literal_datatype
Truth table25%1/4
A and B and C
A= $RDF::Trine::Node::Literal::USE_FORMULAEB= defined $dtC= $dt eq "RDF::Trine::Node::Formula"->literal_datatype
ABCresult
0XX0
10X0
1100
1111
79 0 return RDF::Trine::Node::Formula->new( $literal );
80 } else {
81 2360 return $class->_new( $literal, $lang, $dt );
82 }
83 }
84  
85 2360 ▶sub _new {
Subroutine _new: called CC=6 SCAR=17.9
86 2360 my $class = shift;
87 2360 my $literal = shift;
88 2360 my $lang = shift;
89 2360 my $dt = shift;
90 2360 my $self;
91  
92 2360 ▶ if ($lang and $dt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang and $dt) 1 2359 2360
Condition100%3/3
$lang and $dt
!ll&&!rl&&r
2270891
MC/DC100%2/2
$lang and $dt
$lang $dt
Truth table100%3/3
A and B
A= $langB= $dt
ABresult
0X0
100
111
93 1 throw RDF::Trine::Error::MethodInvocationError ( -text => "Literal values cannot have both language and datatype" );
94 }
95
96 2359 ▶ if ($lang) {
Branch: 2 branches
BranchTrueFalseTotal
if ($lang) { } 89 2270 2359
elsif ($dt) { } 372 1898 2270
97 89 my $oldlang = $lang;
98 # http://tools.ietf.org/html/bcp47#section-2.1.1
99 # All subtags use lowercase letters
100 89 $lang = lc($lang);
101  
102 # with 2 exceptions: subtags that neither appear at the start of the tag nor occur after singletons
103 # i.e. there's a subtag of length at least 2 preceding the exception; and a following subtag or end-of-tag
104  
105 # 1. two-letter subtags are all uppercase
106 89 $lang =~ s{(?<=\w\w-)(\w\w)(?=($|-))}{\U$1}g;
107  
108 # 2. four-letter subtags are titlecase
109 89 $lang =~ s{(?<=\w\w-)(\w\w\w\w)(?=($|-))}{\u\L$1}g;
110 89 $self = [ $literal, $lang, undef ];
111 } elsif ($dt) {
112 372 ▶ if (blessed($dt)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($dt)) 0 372 372
113 0 $dt = $dt->uri_value;
114 }
115 372 $self = [ $literal, undef, $dt ];
116 } else {
117 1898 $self = [ $literal ];
118 }
119 2359 return bless($self, $class);
120 }
121  
122  
123 =item C<< literal_value >>
124  
125 Returns the string value of the literal.
126  
127 =cut
128  
129 8270 ▶sub literal_value {
Subroutine literal_value: called CC=2 SCAR=6.9
Pod documented
130 8270 my $self = shift;
131 8270 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 8269 8270
132 1 $self->[0] = shift;
133 }
134 8270 return $self->[0];
135 }
136  
137 =item C<< literal_value_language >>
138  
139 Returns the language tag of the ltieral.
140  
141 =cut
142  
143 7973 ▶sub literal_value_language {
Subroutine literal_value_language: called CC=1 SCAR=0.0
Pod documented
144 7973 my $self = shift;
145 7973 return $self->[1];
146 }
147  
148 =item C<< literal_datatype >>
149  
150 Returns the datatype of the literal.
151  
152 =cut
153  
154 7464 ▶sub literal_datatype {
Subroutine literal_datatype: called CC=1 SCAR=0.0
Pod documented
155 7464 my $self = shift;
156 7464 return $self->[2];
157 }
158  
159 =item C<< value >>
160  
161 Returns the literal value.
162  
163 =cut
164  
165 590 ▶sub value {
Subroutine value: called CC=1 SCAR=0.0
Pod documented
166 590 my $self = shift;
167 590 return $self->literal_value;
168 }
169  
170 =item C<< sse >>
171  
172 Returns the SSE string for this literal.
173  
174 =cut
175  
176 3458 ▶sub sse {
Subroutine sse: called CC=3 SCAR=11.0
Pod documented
177 3458 my $self = shift;
178 3458 my $literal = $self->literal_value;
179 3458 my $escaped = $self->_unicode_escape( $literal );
180 3458 $literal = $escaped;
181 3458 ▶ if (defined(my $lang = $self->literal_value_language)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $lang = $self->literal_value_language)) { } 162 3296 3458
elsif (defined(my $dt = $self->literal_datatype)) { } 803 2493 3296
182 162 return qq("${literal}"\@${lang});
183 } elsif (defined(my $dt = $self->literal_datatype)) {
184 803 return qq("${literal}"^^<${dt}>);
185 } else {
186 2493 return qq("${literal}");
187 }
188 }
189  
190 =item C<< as_string >>
191  
192 Returns a string representation of the node.
193  
194 =cut
195  
196 2613 ▶sub as_string {
Subroutine as_string: called CC=3 SCAR=11.0
Pod documented
197 2613 my $self = shift;
198 2613 my $string = '"' . $self->literal_value . '"';
199 2613 ▶ if (defined(my $dt = $self->literal_datatype)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $dt = $self->literal_datatype)) { } 587 2026 2613
elsif (defined(my $lang = $self->literal_value_language)) { } 159 1867 2026
200 587 $string .= '^^<' . $dt . '>';
201 } elsif (defined(my $lang = $self->literal_value_language)) {
202 159 $string .= '@' . $lang;
203 }
204 2613 return $string;
205 }
206  
207 =item C<< as_ntriples >>
208  
209 Returns the node in a string form suitable for NTriples serialization.
210  
211 =cut
212  
213 649 ▶sub as_ntriples {
Subroutine as_ntriples: called CC=3 SCAR=11.0
Pod documented
214 649 my $self = shift;
215 649 my $literal = $self->literal_value;
216 649 my $escaped = $self->_unicode_escape( $literal );
217 649 $literal = $escaped;
218 649 ▶ if (defined(my $lang = $self->literal_value_language)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined(my $lang = $self->literal_value_language)) { } 22 627 649
elsif (defined(my $dt = $self->literal_datatype)) { } 162 465 627
219 22 return qq("${literal}"\@${lang});
220 } elsif (defined(my $dt = $self->literal_datatype)) {
221 162 return qq("${literal}"^^<${dt}>);
222 } else {
223 465 return qq("${literal}");
224 }
225 }
226  
227 =item C<< type >>
228  
229 Returns the type string of this node.
230  
231 =cut
232  
233 621 ▶sub type {
Subroutine type: called CC=1 SCAR=0.0
Pod documented
234 621 return 'LITERAL';
235 }
236  
237 =item C<< has_language >>
238  
239 Returns true if this literal is language-tagged, false otherwise.
240  
241 =cut
242  
243 568 ▶sub has_language {
Subroutine has_language: called CC=2 SCAR=6.9
Pod documented
244 568 my $self = shift;
245 568 ▶ return defined($self->literal_value_language) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->literal_value_language ? : 540 28 568
246 }
247  
248 =item C<< has_datatype >>
249  
250 Returns true if this literal is datatyped, false otherwise.
251  
252 =cut
253  
254 108 ▶sub has_datatype {
Subroutine has_datatype: called CC=2 SCAR=6.9
Pod documented
255 108 my $self = shift;
256 108 ▶ return defined($self->literal_datatype) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->literal_datatype ? : 46 62 108
257 }
258  
259 =item C<< equal ( $node ) >>
260  
261 Returns true if the two nodes are equal, false otherwise.
262  
263 =cut
264  
265 17 ▶sub equal {
Subroutine equal: called CC=10 SCAR=23.0
Pod documented
266 17 my $self = shift;
267 17 my $node = shift;
268 17 ▶ return 0 unless (blessed($node) and $node->isa('RDF::Trine::Node::Literal'));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($node) and $node->isa("RDF::Trine::Node::Literal") 6 11 17
Condition100%3/3
builtin::blessed($node) and $node->isa("RDF::Trine::Node::Literal")
!ll&&!rl&&r
1511
MC/DC100%2/2
builtin::blessed($node) and $node->isa("RDF::Trine::Node::Literal")
builtin::blessed($node) $node->isa("RDF::Trine::Node::Literal")
Truth table100%3/3
A and B
A= builtin::blessed($node)B= $node->isa("RDF::Trine::Node::Literal")
ABresult
0X0
100
111
269 11 ▶ return 0 unless ($self->literal_value eq $node->literal_value);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->literal_value eq $node->literal_value 1 10 11
270 10 ▶ if ($self->literal_datatype or $node->literal_datatype) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->literal_datatype or $node->literal_datatype) 4 6 10
Condition66%2/3
$self->literal_datatype or $node->literal_datatype
l!l&&r!l&&!r
406
MC/DC50%1/2
$self->literal_datatype or $node->literal_datatype
$self->literal_datatype $node->literal_datatype
Truth table66%2/3
A or B
A= $self->literal_datatypeB= $node->literal_datatype
ABresult
1X1
011
000
271 68 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
272 4 ▶ return 0 unless ($self->literal_datatype eq $node->literal_datatype);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->literal_datatype eq $node->literal_datatype 1 3 4
273 }
274 9 ▶ if ($self->literal_value_language or $node->literal_value_language) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->literal_value_language or $node->literal_value_language) 3 6 9
Condition66%2/3
$self->literal_value_language or $node->literal_value_language
l!l&&r!l&&!r
306
MC/DC50%1/2
$self->literal_value_language or $node->literal_value_language
$self->literal_value_language $node->literal_value_language
Truth table66%2/3
A or B
A= $self->literal_value_languageB= $node->literal_value_language
ABresult
1X1
011
000
275 68 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
276 3 ▶ return 0 unless ($self->literal_value_language eq $node->literal_value_language);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->literal_value_language eq $node->literal_value_language 1 2 3
277 }
278 8 return 1;
279 }
280  
281 # called to compare two nodes of the same type
282 144 ▶sub _compare {
Subroutine _compare: called CC=8 SCAR=37.5
283 144 my $a = shift;
284 144 my $b = shift;
285 144 ▶ if ($a->literal_value ne $b->literal_value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a->literal_value ne $b->literal_value) 144 0 144
286 144 return ($a->literal_value cmp $b->literal_value);
287 }
288
289 # the nodes have the same lexical value
290 0 ▶ if ($a->has_language and $b->has_language) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a->has_language and $b->has_language) 0 0 0
Condition0%0/3
$a->has_language and $b->has_language
!ll&&!rl&&r
000
MC/DC0%0/2
$a->has_language and $b->has_language
$a->has_language $b->has_language
Truth table0%0/3
A and B
A= $a->has_languageB= $b->has_language
ABresult
0X0
100
111
291 0 return ($a->literal_value_language cmp $b->literal_value_language);
292 }
293
294 0 ▶ if ($a->has_datatype and $b->has_datatype) {
Branch: 3 branches
BranchTrueFalseTotal
if ($a->has_datatype and $b->has_datatype) { } 0 0 0
elsif ($a->has_datatype) { } 0 0 0
elsif ($b->has_datatype) { } 0 0 0
Condition0%0/3
$a->has_datatype and $b->has_datatype
!ll&&!rl&&r
000
MC/DC0%0/2
$a->has_datatype and $b->has_datatype
$a->has_datatype $b->has_datatype
Truth table0%0/3
A and B
A= $a->has_datatypeB= $b->has_datatype
ABresult
0X0
100
111
295 0 return ($a->literal_datatype cmp $b->literal_datatype);
296 } elsif ($a->has_datatype) {
297 0 return 1;
298 } elsif ($b->has_datatype) {
299 0 return -1;
300 }
301
302 0 return 0;
303 }
304  
305 =item C<< canonicalize >>
306  
307 Returns a new literal node object whose value is in canonical form (where applicable).
308  
309 =cut
310  
311 588 ▶sub canonicalize {
Subroutine canonicalize: called CC=2 SCAR=6.9
Pod documented
312 588 my $self = shift;
313 588 my $class = ref($self);
314 588 my $dt = $self->literal_datatype;
315 588 my $lang = $self->literal_value_language;
316 588 my $value = $self->value;
317 588 ▶ if (defined $dt) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dt) 5 583 588
318 5 $value = RDF::Trine::Node::Literal->canonicalize_literal_value( $value, $dt, 1 );
319 }
320 588 return $class->new($value, $lang, $dt);
321 }
322  
323 =item C<< canonicalize_literal_value ( $string, $datatype, $warn ) >>
324  
325 If C<< $datatype >> is a recognized datatype, returns the canonical lexical
326 representation of the value C<< $string >>. Otherwise returns C<< $string >>.
327  
328 Currently, xsd:integer, xsd:decimal, and xsd:boolean are canonicalized.
329 Additionally, invalid lexical forms for xsd:float, xsd:double, and xsd:dateTime
330 will trigger a warning.
331  
332 =cut
333  
334 40 ▶sub canonicalize_literal_value {
Subroutine canonicalize_literal_value: called CC=36 SCAR=43.5
Pod documented
335 40 my $self = shift;
336 40 my $value = shift;
337 40 my $dt = shift;
338 40 my $warn = shift;
339
340 40 ▶ if ($dt eq 'http://www.w3.org/2001/XMLSchema#integer') {
Branch: 6 branches
BranchTrueFalseTotal
if ($dt eq 'http://www.w3.org/2001/XMLSchema#integer') { } 9 31 40
elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#decimal') { } 5 26 31
elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#float') { } 8 18 26
elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#double') { } 8 10 18
elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#boolean') { } 10 0 10
elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#dateTime') { } 0 0 0
341 9 ▶ if ($value =~ m/^([-+])?(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^([-+])?(\d+)$/) { } 9 0 9
342 9 ▶ my $sign = $1 || '';
Condition100%2/2
$1 || ''
l!l
81
MC/DC100%1/1
$1 || ''
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
343 9 my $num = $2;
344 9 ▶ $sign = '' if ($sign eq '+');
Branch: 1 branch
BranchTrueFalseTotal
if $sign eq "+" 4 5 9
345 9 $num =~ s/^0+(\d)/$1/;
346 9 return "${sign}${num}";
347 } else {
348 0 ▶ warn "Bad lexical form for xsd:integer: '$value'" if ($warn);
Branch: 1 branch
BranchTrueFalseTotal
if $warn 0 0 0
349 }
350 } elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#decimal') {
351 5 ▶ if ($value =~ m/^([-+])?((\d+)([.]\d*)?)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value =~ /^([-+])?((\d+)([.]\d*)?)$/) { } 5 0 5
elsif ($value =~ /^([-+])?([.]\d+)$/) { } 0 0 0
352 5 ▶ my $sign = $1 || '';
Condition100%2/2
$1 || ''
l!l
41
MC/DC100%1/1
$1 || ''
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
353 5 my $num = $2;
354 5 my $int = $3;
355 5 my $frac = $4;
356 5 ▶ $sign = '' if ($sign eq '+');
Branch: 1 branch
BranchTrueFalseTotal
if $sign eq "+" 4 1 5
357 5 $num =~ s/^0+(.)/$1/;
358 5 $num =~ s/[.](\d)0+$/.$1/;
359 5 ▶ if ($num =~ /^[.]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num =~ /^[.]/) 1 4 5
360 1 $num = "0$num";
361 }
362 5 ▶ if ($num !~ /[.]/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($num =~ /[.]/) 0 5 5
363 0 $num = "${num}.0";
364 }
365 5 return "${sign}${num}";
366 } elsif ($value =~ m/^([-+])?([.]\d+)$/) {
367 0 ▶ my $sign = $1 || '';
Condition0%0/2
$1 || ''
l!l
00
MC/DC0%0/1
$1 || ''
$1
Truth table0%0/2
A
A= $1
Aresult
11
00
368 0 my $num = $2;
369 0 ▶ $sign = '' if ($sign eq '+');
Branch: 1 branch
BranchTrueFalseTotal
if $sign eq "+" 0 0 0
370 0 $num =~ s/^0+(.)/$1/;
371 0 return "${sign}${num}";
372 } else {
373 0 ▶ warn "Bad lexical form for xsd:deciaml: '$value'" if ($warn);
Branch: 1 branch
BranchTrueFalseTotal
if $warn 0 0 0
374 0 $value = sprintf('%f', $value);
375 }
376 } elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#float') {
377 8 ▶ if ($value =~ m/^(?:([-+])?(?:(\d+(?:\.\d*)?|\.\d+)([Ee][-+]?\d+)?|(INF)))|(NaN)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^(?:([-+])?(?:(\d+(?:\.\d*)?|\.\d+)([Ee][-+]?\d+)?|(INF)))|(NaN)$/) { } 8 0 8
378 8 my $sign = $1;
379 8 my $inf = $4;
380 8 my $nan = $5;
381 68 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
382 8 ▶ $sign = '' if ($sign eq '+');
Branch: 1 branch
BranchTrueFalseTotal
if $sign eq "+" 2 6 8
383 8 ▶ return "${sign}$inf" if ($inf);
Branch: 1 branch
BranchTrueFalseTotal
if $inf 2 6 8
384 6 ▶ return $nan if ($nan);
Branch: 1 branch
BranchTrueFalseTotal
if $nan 1 5 6
385  
386 5 $value = sprintf('%E', $value);
387 5 $value =~ m/^(?:([-+])?(?:(\d+(?:\.\d*)?|\.\d+)([Ee][-+]?\d+)?|(INF)))|(NaN)$/;
388 5 $sign = $1;
389 5 $inf = $4;
390 5 $nan = $5;
391 5 my $num = $2;
392 5 my $exp = $3;
393 5 $num =~ s/[.](\d+?)0+/.$1/;
394 5 $exp =~ tr/e/E/;
395 5 $exp =~ s/E[+]/E/;
396 5 $exp =~ s/E(-?)0+([1-9])$/E$1$2/;
397 5 $exp =~ s/E(-?)0+$/E${1}0/;
398 5 return "${sign}${num}${exp}";
399 } else {
400 0 ▶ warn "Bad lexical form for xsd:float: '$value'" if ($warn);
Branch: 1 branch
BranchTrueFalseTotal
if $warn 0 0 0
401 0 $value = sprintf('%E', $value);
402 0 $value =~ s/E[+]/E/;
403 0 $value =~ s/E0+(\d)/E$1/;
404 0 $value =~ s/(\d)0+E/$1E/;
405 }
406 } elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#double') {
407 8 ▶ if ($value =~ m/^(?:([-+])?(?:(\d+(?:\.\d*)?|\.\d+)([Ee][-+]?\d+)?|(INF)))|(NaN)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^(?:([-+])?(?:(\d+(?:\.\d*)?|\.\d+)([Ee][-+]?\d+)?|(INF)))|(NaN)$/) { } 8 0 8
408 8 my $sign = $1;
409 8 my $inf = $4;
410 8 my $nan = $5;
411 68 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
412 8 ▶ $sign = '' if ($sign eq '+');
Branch: 1 branch
BranchTrueFalseTotal
if $sign eq "+" 2 6 8
413 8 ▶ return "${sign}$inf" if ($inf);
Branch: 1 branch
BranchTrueFalseTotal
if $inf 2 6 8
414 6 ▶ return $nan if ($nan);
Branch: 1 branch
BranchTrueFalseTotal
if $nan 1 5 6
415  
416 5 $value = sprintf('%E', $value);
417 5 $value =~ m/^(?:([-+])?(?:(\d+(?:\.\d*)?|\.\d+)([Ee][-+]?\d+)?|(INF)))|(NaN)$/;
418 5 $sign = $1;
419 5 $inf = $4;
420 5 $nan = $5;
421 5 my $num = $2;
422 5 my $exp = $3;
423 5 $num =~ s/[.](\d+?)0+/.$1/;
424 5 $exp =~ tr/e/E/;
425 5 $exp =~ s/E[+]/E/;
426 5 $exp =~ s/E(-?)0+([1-9])$/E$1$2/;
427 5 $exp =~ s/E(-?)0+$/E${1}0/;
428 5 return "${sign}${num}${exp}";
429 } else {
430 0 ▶ warn "Bad lexical form for xsd:double: '$value'" if ($warn);
Branch: 1 branch
BranchTrueFalseTotal
if $warn 0 0 0
431 0 $value = sprintf('%E', $value);
432 0 $value =~ s/E[+]/E/;
433 0 $value =~ s/E0+(\d)/E$1/;
434 0 $value =~ s/(\d)0+E/$1E/;
435 }
436 } elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#boolean') {
437 10 ▶ if ($value =~ m/^(true|false|0|1)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^(true|false|0|1)$/) { } 10 0 10
438 10 ▶ $value = 'true' if ($value eq '1');
Branch: 1 branch
BranchTrueFalseTotal
if $value eq 1 4 6 10
439 10 ▶ $value = 'false' if ($value eq '0');
Branch: 1 branch
BranchTrueFalseTotal
if $value eq 0 4 6 10
440 10 return $value;
441 } else {
442 0 ▶ warn "Bad lexical form for xsd:boolean: '$value'" if ($warn);
Branch: 1 branch
BranchTrueFalseTotal
if $warn 0 0 0
443 }
444 } elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#dateTime') {
445 0 ▶ if ($value =~ m/^-?([1-9]\d{3,}|0\d{3})-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])T(([01]\d|2[0-3]):[0-5]\d:[0-5]\d(\.\d+)?|(24:00:00(\.0+)?))(Z|(\+|-)((0\d|1[0-3]):[0-5]\d|14:00))?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^-?([1-9]\d{3,}|0\d{3})-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])T(([01]\d|2[0-3]):[0-5]\d:[0-5]\d(\.\d+)?|(24:00:00(\.0+)?))(Z|(\+|-)((0\d|1[0-3]):[0-5]\d|14:00))?$/) { } 0 0 0
446 # XXX need to canonicalize the dateTime
447 0 return $value;
448 } else {
449 0 ▶ warn "Bad lexical form for xsd:boolean: '$value'" if ($warn);
Branch: 1 branch
BranchTrueFalseTotal
if $warn 0 0 0
450 }
451 }
452 0 return $value;
453 }
454  
455 =item C<< is_canonical_lexical_form >>
456  
457 =cut
458  
459 24 ▶sub is_canonical_lexical_form {
Subroutine is_canonical_lexical_form: called CC=17 SCAR=37.7
Pod documented
460 24 my $self = shift;
461 24 my $value = $self->literal_value;
462 24 my $dt = $self->literal_datatype;
463
464 24 ▶ unless ($dt =~ qr<^http://www.w3.org/2001/XMLSchema#(integer|decimal|float|double|boolean|dateTime|non(Positive|Negative)Integer|(positive|negative)Integer|long|int|short|byte|unsigned(Long|Int|Short|Byte))>) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dt =~ qr"^http://www.w3.org/2001/XMLSchema#(integer|decimal|float|double|boolean|dateTime|non(Positive|Negative)Integer|(positive|negative)Integer|long|int|short|byte|unsigned(Long|Int|Short|Byte))") 0 24 24
465 0 return '0E0'; # zero but true (it's probably ok, but we don't recognize the datatype)
466 }
467
468 24 ▶ if ($dt =~ m<http://www.w3.org/2001/XMLSchema#(integer|non(Positive|Negative)Integer|(positive|negative)Integer|long|int|short|byte|unsigned(Long|Int|Short|Byte))>) {
Branch: 6 branches
BranchTrueFalseTotal
if ($dt =~ m[http://www.w3.org/2001/XMLSchema#(integer|non(Positive|Negative)Integer|(positive|negative)Integer|long|int|short|byte|unsigned(Long|Int|Short|Byte))]) { } 18 6 24
elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#decimal') { } 3 3 6
elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#float') { } 0 3 3
elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#double') { } 3 0 3
elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#boolean') { } 0 0 0
elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#dateTime') { } 0 0 0
469 18 ▶ if ($value =~ m/^([-+])?(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^([-+])?(\d+)$/) { } 17 1 18
470 17 return 1;
471 } else {
472 1 return 0;
473 }
474 } elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#decimal') {
475 3 ▶ if ($value =~ m/^([-+])?((\d+)[.]\d+)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value =~ /^([-+])?((\d+)[.]\d+)$/) { } 1 2 3
elsif ($value =~ /^([-+])?([.]\d+)$/) { } 1 1 2
476 1 return 1;
477 } elsif ($value =~ m/^([-+])?([.]\d+)$/) {
478 1 return 1;
479 } else {
480 1 return 0;
481 }
482 } elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#float') {
483 0 ▶ if ($value =~ m/^[-+]?(\d+\.\d*|\.\d+)([Ee][-+]?\d+)?|[-+]?INF|NaN$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value =~ /^[-+]?(\d+\.\d*|\.\d+)([Ee][-+]?\d+)?|[-+]?INF|NaN$/) { } 0 0 0
elsif ($value =~ /^[-+]?(\d+(\.\d*)?|\.\d+)([Ee][-+]?\d+)|[-+]?INF|NaN$/) { } 0 0 0
484 0 return 1;
485 } elsif ($value =~ m/^[-+]?(\d+(\.\d*)?|\.\d+)([Ee][-+]?\d+)|[-+]?INF|NaN$/) {
486 0 return 1;
487 } else {
488 0 return 0;
489 }
490 } elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#double') {
491 3 ▶ if ($value =~ m/^[-+]?((\d+(\.\d*))|(\.\d+))([Ee][-+]?\d+)?|[-+]?INF|NaN$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value =~ /^[-+]?((\d+(\.\d*))|(\.\d+))([Ee][-+]?\d+)?|[-+]?INF|NaN$/) { } 1 2 3
elsif ($value =~ /^[-+]?((\d+(\.\d*)?)|(\.\d+))([Ee][-+]?\d+)|[-+]?INF|NaN$/) { } 1 1 2
492 1 return 1;
493 } elsif ($value =~ m/^[-+]?((\d+(\.\d*)?)|(\.\d+))([Ee][-+]?\d+)|[-+]?INF|NaN$/) {
494 1 return 1;
495 } else {
496 1 return 0;
497 }
498 } elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#boolean') {
499 0 ▶ if ($value =~ m/^(true|false)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^(true|false)$/) { } 0 0 0
500 0 return 1;
501 } else {
502 0 return 0;
503 }
504 } elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#dateTime') {
505 0 ▶ if ($value =~ m/^-?([1-9]\d{3,}|0\d{3})-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])T(([01]\d|2[0-3]):[0-5]\d:[0-5]\d(\.\d+)?|(24:00:00(\.0+)?))(Z|(\+|-)((0\d|1[0-3]):[0-5]\d|14:00))?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^-?([1-9]\d{3,}|0\d{3})-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])T(([01]\d|2[0-3]):[0-5]\d:[0-5]\d(\.\d+)?|(24:00:00(\.0+)?))(Z|(\+|-)((0\d|1[0-3]):[0-5]\d|14:00))?$/) { } 0 0 0
506 0 return 1;
507 } else {
508 0 return 0;
509 }
510 }
511 0 return 0;
512 }
513  
514 =item C<< is_valid_lexical_form >>
515  
516 Returns true if the node is of a recognized datatype and has a valid lexical form
517 for that datatype. If the lexical form is invalid, returns false. If the datatype
518 is unrecognized, returns zero-but-true.
519  
520 =cut
521  
522 5 ▶sub is_valid_lexical_form {
Subroutine is_valid_lexical_form: called CC=15 SCAR=47.9
Pod documented
523 5 my $self = shift;
524 5 my $value = $self->literal_value;
525 5 my $dt = $self->literal_datatype;
526
527 5 ▶ unless ($dt =~ qr<^http://www.w3.org/2001/XMLSchema#(integer|decimal|float|double|boolean|dateTime|non(Positive|Negative)Integer|(positive|negative)Integer|long|int|short|byte|unsigned(Long|Int|Short|Byte))>) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dt =~ qr"^http://www.w3.org/2001/XMLSchema#(integer|decimal|float|double|boolean|dateTime|non(Positive|Negative)Integer|(positive|negative)Integer|long|int|short|byte|unsigned(Long|Int|Short|Byte))") 0 5 5
528 0 return '0E0'; # zero but true (it's probably ok, but we don't recognize the datatype)
529 }
530
531 5 ▶ if ($dt =~ m<http://www.w3.org/2001/XMLSchema#(integer|non(Positive|Negative)Integer|(positive|negative)Integer|long|int|short|byte|unsigned(Long|Int|Short|Byte))>) {
Branch: 6 branches
BranchTrueFalseTotal
if ($dt =~ m[http://www.w3.org/2001/XMLSchema#(integer|non(Positive|Negative)Integer|(positive|negative)Integer|long|int|short|byte|unsigned(Long|Int|Short|Byte))]) { } 5 0 5
elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#decimal') { } 0 0 0
elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#float') { } 0 0 0
elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#double') { } 0 0 0
elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#boolean') { } 0 0 0
elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#dateTime') { } 0 0 0
532 5 ▶ if ($value =~ m/^([-+])?(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^([-+])?(\d+)$/) { } 3 2 5
533 3 return 1;
534 } else {
535 2 return 0;
536 }
537 } elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#decimal') {
538 0 ▶ if ($value =~ m/^([-+])?((\d+)([.]\d*)?)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value =~ /^([-+])?((\d+)([.]\d*)?)$/) { } 0 0 0
elsif ($value =~ /^([-+])?([.]\d+)$/) { } 0 0 0
539 0 return 1;
540 } elsif ($value =~ m/^([-+])?([.]\d+)$/) {
541 0 return 1;
542 } else {
543 0 return 0;
544 }
545 } elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#float') {
546 0 ▶ if ($value =~ m/^[-+]?(\d+(\.\d*)?|\.\d+)([Ee][-+]?\d+)?|[-+]?INF|NaN$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^[-+]?(\d+(\.\d*)?|\.\d+)([Ee][-+]?\d+)?|[-+]?INF|NaN$/) { } 0 0 0
547 0 return 1;
548 } else {
549 0 return 0;
550 }
551 } elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#double') {
552 0 ▶ if ($value =~ m/^[-+]?((\d+(\.\d*)?)|(\.\d+))([Ee][-+]?\d+)?|[-+]?INF|NaN$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^[-+]?((\d+(\.\d*)?)|(\.\d+))([Ee][-+]?\d+)?|[-+]?INF|NaN$/) { } 0 0 0
553 0 return 1;
554 } else {
555 0 return 0;
556 }
557 } elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#boolean') {
558 0 ▶ if ($value =~ m/^(true|false|0|1)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^(true|false|0|1)$/) { } 0 0 0
559 0 return 1;
560 } else {
561 0 return 0;
562 }
563 } elsif ($dt eq 'http://www.w3.org/2001/XMLSchema#dateTime') {
564 0 ▶ if ($value =~ m/^-?([1-9]\d{3,}|0\d{3})-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])T(([01]\d|2[0-3]):[0-5]\d:[0-5]\d(\.\d+)?|(24:00:00(\.0+)?))(Z|(\+|-)((0\d|1[0-3]):[0-5]\d|14:00))?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^-?([1-9]\d{3,}|0\d{3})-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])T(([01]\d|2[0-3]):[0-5]\d:[0-5]\d(\.\d+)?|(24:00:00(\.0+)?))(Z|(\+|-)((0\d|1[0-3]):[0-5]\d|14:00))?$/) { } 0 0 0
565 0 return 1;
566 } else {
567 0 return 0;
568 }
569 }
570 0 return 0;
571 }
572  
573 =item C<< is_numeric_type >>
574  
575 Returns true if the literal is a known (xsd) numeric type.
576  
577 =cut
578  
579 0 ▶sub is_numeric_type {
Subroutine is_numeric_type: not called CC=3 SCAR=24.8
Pod documented
580 0 my $self = shift;
581 0 ▶ return 0 unless ($self->has_datatype);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_datatype 0 0 0
582 0 my $type = $self->literal_datatype;
583 0 ▶ if ($type =~ qr<^http://www.w3.org/2001/XMLSchema#(integer|decimal|float|double|non(Positive|Negative)Integer|(positive|negative)Integer|long|int|short|byte|unsigned(Long|Int|Short|Byte))>) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ qr"^http://www.w3.org/2001/XMLSchema#(integer|decimal|float|double|non(Positive|Negative)Integer|(positive|negative)Integer|long|int|short|byte|unsigned(Long|Int|Short|Byte))") { } 0 0 0
584 0 return 1;
585 } else {
586 0 return 0;
587 }
588 }
589  
590 =item C<< numeric_value >>
591  
592 Returns the numeric value of the literal (even if the literal isn't a known numeric type.
593  
594 =cut
595  
596 0 ▶sub numeric_value {
Subroutine numeric_value: not called CC=7 SCAR=40.3
Pod documented
597 0 my $self = shift;
598 0 ▶ if ($self->is_numeric_type) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->is_numeric_type) { } 0 0 0
elsif (not $self->has_datatype) { } 0 0 0
elsif ($self->literal_datatype eq 'http://www.w3.org/2001/XMLSchema#boolean') { } 0 0 0
599 0 my $value = $self->literal_value;
600 0 ▶ if (looks_like_number($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($value)) { } 0 0 0
601 0 my $v = 0 + eval "$value"; ## no critic (ProhibitStringyEval)
602 0 return $v;
603 } else {
604 0 throw RDF::Query::Error::TypeError -text => "Literal with numeric type does not appear to have numeric value.";
605 }
606 } elsif (not $self->has_datatype) {
607 0 ▶ if (looks_like_number($self->literal_value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number(scalar $self->literal_value)) { } 0 0 0
608 0 return 0+$self->literal_value;
609 } else {
610 0 return;
611 }
612 } elsif ($self->literal_datatype eq 'http://www.w3.org/2001/XMLSchema#boolean') {
613 0 ▶ return ($self->literal_value eq 'true') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->literal_value eq 'true' ? : 0 0 0
614 } else {
615 0 return;
616 }
617 }
618  
619 68 1;
620  
621 __END__
« RDF/Trine/Store/DBI/Pg.pm ↑ Summary RDF/Trine/VariableBindings.pm »