source: gs2-extensions/parallel-building/trunk/src/perllib/cpan/XML/XPath/Number.pm@ 24626

Last change on this file since 24626 was 24626, checked in by jmt12, 13 years ago

An (almost) complete copy of the perllib directory from a (circa SEP2011) head checkout from Greenstone 2 trunk - in order to try and make merging in this extension a little easier later on (as there have been some major changes to buildcol.pl commited in the main trunk but not in the x64 branch)

File size: 1.6 KB
Line 
1# $Id: Number.pm 7909 2004-08-06 05:11:55Z mdewsnip $
2
3package XML::XPath::Number;
4use XML::XPath::Boolean;
5use XML::XPath::Literal;
6use strict;
7
8use overload
9 '""' => \&value,
10 '0+' => \&value,
11 '<=>' => \&cmp;
12
13sub new {
14 my $class = shift;
15 my $number = shift;
16 if ($number !~ /^\s*[+-]?(\d+(\.\d*)?|\.\d+)\s*$/) {
17 $number = undef;
18 }
19 else {
20 $number =~ s/^\s*(.*)\s*$/$1/;
21 }
22 bless \$number, $class;
23}
24
25sub as_string {
26 my $self = shift;
27 defined $$self ? $$self : 'NaN';
28}
29
30sub as_xml {
31 my $self = shift;
32 return "<Number>" . (defined($$self) ? $$self : 'NaN') . "</Number>\n";
33}
34
35sub value {
36 my $self = shift;
37 $$self;
38}
39
40sub cmp {
41 my $self = shift;
42 my ($other, $swap) = @_;
43 if ($swap) {
44 return $other <=> $$self;
45 }
46 return $$self <=> $other;
47}
48
49sub evaluate {
50 my $self = shift;
51 $self;
52}
53
54sub to_boolean {
55 my $self = shift;
56 return $$self ? XML::XPath::Boolean->True : XML::XPath::Boolean->False;
57}
58
59sub to_literal { XML::XPath::Literal->new($_[0]->as_string); }
60sub to_number { $_[0]; }
61
62sub string_value { return $_[0]->value }
63
641;
65__END__
66
67=head1 NAME
68
69XML::XPath::Number - Simple numeric values.
70
71=head1 DESCRIPTION
72
73This class holds simple numeric values. It doesn't support -0, +/- Infinity,
74or NaN, as the XPath spec says it should, but I'm not hurting anyone I don't think.
75
76=head1 API
77
78=head2 new($num)
79
80Creates a new XML::XPath::Number object, with the value in $num. Does some
81rudimentary numeric checking on $num to ensure it actually is a number.
82
83=head2 value()
84
85Also as overloaded stringification. Returns the numeric value held.
86
87=cut
Note: See TracBrowser for help on using the repository browser.