| 1 |
# $Id$ |
| 2 |
# |
| 3 |
# This is free software, you may use it and distribute it under the same terms as |
| 4 |
# Perl itself. |
| 5 |
# |
| 6 |
# Copyright 2001-2003 AxKit.com Ltd., 2002-2006 Christian Glahn, 2006-2009 Petr Pajas |
| 7 |
# |
| 8 |
# |
| 9 |
|
| 10 |
package XML::LibXML::Number; |
| 11 |
use XML::LibXML::Boolean; |
| 12 |
use XML::LibXML::Literal; |
| 13 |
use strict; |
| 14 |
use warnings; |
| 15 |
|
| 16 |
use vars qw ($VERSION); |
| 17 |
$VERSION = "2.0134"; # VERSION TEMPLATE: DO NOT CHANGE |
| 18 |
|
| 19 |
use overload |
| 20 |
'""' => \&value, |
| 21 |
'0+' => \&value, |
| 22 |
'<=>' => \&cmp; |
| 23 |
|
| 24 |
sub new { |
| 25 |
my $class = shift; |
| 26 |
my $number = shift; |
| 27 |
if ($number !~ /^\s*(-\s*)?(\d+(\.\d*)?|\.\d+)\s*$/) { |
| 28 |
$number = undef; |
| 29 |
} |
| 30 |
else { |
| 31 |
$number =~ s/\s+//g; |
| 32 |
} |
| 33 |
bless \$number, $class; |
| 34 |
} |
| 35 |
|
| 36 |
sub as_string { |
| 37 |
my $self = shift; |
| 38 |
defined $$self ? $$self : 'NaN'; |
| 39 |
} |
| 40 |
|
| 41 |
sub as_xml { |
| 42 |
my $self = shift; |
| 43 |
return "<Number>" . (defined($$self) ? $$self : 'NaN') . "</Number>\n"; |
| 44 |
} |
| 45 |
|
| 46 |
sub value { |
| 47 |
my $self = shift; |
| 48 |
$$self; |
| 49 |
} |
| 50 |
|
| 51 |
sub cmp { |
| 52 |
my $self = shift; |
| 53 |
my ($other, $swap) = @_; |
| 54 |
if ($swap) { |
| 55 |
return $other <=> $$self; |
| 56 |
} |
| 57 |
return $$self <=> $other; |
| 58 |
} |
| 59 |
|
| 60 |
sub evaluate { |
| 61 |
my $self = shift; |
| 62 |
$self; |
| 63 |
} |
| 64 |
|
| 65 |
sub to_boolean { |
| 66 |
my $self = shift; |
| 67 |
return $$self ? XML::LibXML::Boolean->True : XML::LibXML::Boolean->False; |
| 68 |
} |
| 69 |
|
| 70 |
sub to_literal { XML::LibXML::Literal->new($_[0]->as_string); } |
| 71 |
sub to_number { $_[0]; } |
| 72 |
|
| 73 |
sub string_value { return $_[0]->value } |
| 74 |
|
| 75 |
1; |
| 76 |
__END__ |
| 77 |
|
| 78 |
=head1 NAME |
| 79 |
|
| 80 |
XML::LibXML::Number - Simple numeric values. |
| 81 |
|
| 82 |
=head1 DESCRIPTION |
| 83 |
|
| 84 |
This class holds simple numeric values. It doesn't support -0, +/- Infinity, |
| 85 |
or NaN, as the XPath spec says it should, but I'm not hurting anyone I don't think. |
| 86 |
|
| 87 |
=head1 API |
| 88 |
|
| 89 |
=head2 new($num) |
| 90 |
|
| 91 |
Creates a new XML::LibXML::Number object, with the value in $num. Does some |
| 92 |
rudimentary numeric checking on $num to ensure it actually is a number. |
| 93 |
|
| 94 |
=head2 value() |
| 95 |
|
| 96 |
Also as overloaded stringification. Returns the numeric value held. |
| 97 |
|
| 98 |
=cut |