From a2d12bc84fb2af87dd1c0c6e5bc854554902cd67 Mon Sep 17 00:00:00 2001 From: Lorry Tar Creator Date: Sun, 26 Jan 2003 19:35:03 +0000 Subject: Imported from /home/lorry/working-area/delta_perl-xml-xpath/XML-XPath-1.13.tar.gz. --- XPath/Number.pm | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 XPath/Number.pm (limited to 'XPath/Number.pm') diff --git a/XPath/Number.pm b/XPath/Number.pm new file mode 100644 index 0000000..74fff2e --- /dev/null +++ b/XPath/Number.pm @@ -0,0 +1,87 @@ +# $Id: Number.pm,v 1.14 2002/12/26 17:57:09 matt Exp $ + +package XML::XPath::Number; +use XML::XPath::Boolean; +use XML::XPath::Literal; +use strict; + +use overload + '""' => \&value, + '0+' => \&value, + '<=>' => \&cmp; + +sub new { + my $class = shift; + my $number = shift; + if ($number !~ /^\s*[+-]?(\d+(\.\d*)?|\.\d+)\s*$/) { + $number = undef; + } + else { + $number =~ s/^\s*(.*)\s*$/$1/; + } + bless \$number, $class; +} + +sub as_string { + my $self = shift; + defined $$self ? $$self : 'NaN'; +} + +sub as_xml { + my $self = shift; + return "" . (defined($$self) ? $$self : 'NaN') . "\n"; +} + +sub value { + my $self = shift; + $$self; +} + +sub cmp { + my $self = shift; + my ($other, $swap) = @_; + if ($swap) { + return $other <=> $$self; + } + return $$self <=> $other; +} + +sub evaluate { + my $self = shift; + $self; +} + +sub to_boolean { + my $self = shift; + return $$self ? XML::XPath::Boolean->True : XML::XPath::Boolean->False; +} + +sub to_literal { XML::XPath::Literal->new($_[0]->as_string); } +sub to_number { $_[0]; } + +sub string_value { return $_[0]->value } + +1; +__END__ + +=head1 NAME + +XML::XPath::Number - Simple numeric values. + +=head1 DESCRIPTION + +This class holds simple numeric values. It doesn't support -0, +/- Infinity, +or NaN, as the XPath spec says it should, but I'm not hurting anyone I don't think. + +=head1 API + +=head2 new($num) + +Creates a new XML::XPath::Number object, with the value in $num. Does some +rudimentary numeric checking on $num to ensure it actually is a number. + +=head2 value() + +Also as overloaded stringification. Returns the numeric value held. + +=cut -- cgit v1.2.1