| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## HTML Object - ~/lib/HTML/Object/XPath/Number.pm | ||
| 3 | ## Version v0.2.0 | ||
| 4 | ## Copyright(c) 2021 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2021/12/05 | ||
| 7 | ## Modified 2022/09/18 | ||
| 8 | ## All rights reserved | ||
| 9 | ## | ||
| 10 | ## | ||
| 11 | ## This program is free software; you can redistribute it and/or modify it | ||
| 12 | ## under the same terms as Perl itself. | ||
| 13 | ##---------------------------------------------------------------------------- | ||
| 14 | package HTML::Object::XPath::Number; | ||
| 15 | BEGIN | ||
| 16 | { | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | warnings::register_categories( 'HTML::Object' ); | ||
| 20 | use parent qw( Module::Generic ); | ||
| 21 | use vars qw( $TRUE $FALSE $BASE_CLASS $DEBUG $VERSION ); | ||
| 22 | use HTML::Object::XPath::Boolean; | ||
| 23 | our $TRUE = HTML::Object::XPath::Boolean->True; | ||
| 24 | our $FALSE = HTML::Object::XPath::Boolean->False; | ||
| 25 | our $BASE_CLASS = 'HTML::Object::XPath'; | ||
| 26 | our $DEBUG = 0; | ||
| 27 | our $VERSION = 'v0.2.0'; | ||
| 28 | }; | ||
| 29 | |||
| 30 | use strict; | ||
| 31 | use warnings; | ||
| 32 | |||
| 33 | sub new | ||
| 34 | { | ||
| 35 | my $this = shift( @_ ); | ||
| 36 | my $number = shift( @_ ); | ||
| 37 | if( $number !~ /^[[:blank:]\h]*[+-]?(\d+(\.\d*)?|\.\d+)[[:blank:]\h]*$/ ) | ||
| 38 | { | ||
| 39 | $number = undef; | ||
| 40 | } | ||
| 41 | else | ||
| 42 | { | ||
| 43 | $number =~ s/^[[:blank:]\h]*(.*)[[:blank:]\h]*$/$1/; | ||
| 44 | } | ||
| 45 | return( bless( \$number => ( ref( $this ) || $this ) ) ); | ||
| 46 | } | ||
| 47 | |||
| 48 | sub as_string | ||
| 49 | { | ||
| 50 | my $self = shift( @_ ); | ||
| 51 | return( defined( $$self ) ? $$self : 'NaN' ); | ||
| 52 | } | ||
| 53 | |||
| 54 | sub as_xml | ||
| 55 | { | ||
| 56 | my $self = shift( @_ ); | ||
| 57 | return( "<Number>" . ( defined( $$self ) ? $$self : 'NaN' ) . "</Number>\n" ); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub cmp | ||
| 61 | { | ||
| 62 | my $self = shift( @_ ); | ||
| 63 | my( $other, $swap ) = @_; | ||
| 64 | return( $swap ? $other <=> $$self : $$self <=> $other ); | ||
| 65 | } | ||
| 66 | |||
| 67 | sub evaluate { return( $_[0] ); } | ||
| 68 | |||
| 69 | sub getAttributes { return( wantarray() ? () : [] ); } | ||
| 70 | |||
| 71 | sub getChildNodes { return( wantarray() ? () : [] ); } | ||
| 72 | |||
| 73 | sub new_literal { return( shift->_class_for( 'Literal' )->new( @_ ) ); } | ||
| 74 | |||
| 75 | sub string_value { return $_[0]->value } | ||
| 76 | |||
| 77 | sub to_boolean { return( ${$_[0]} ? $TRUE : $FALSE ); } | ||
| 78 | |||
| 79 | sub to_literal { $_[0]->new_literal( $_[0]->as_string ); } | ||
| 80 | |||
| 81 | sub to_number { $_[0]; } | ||
| 82 | |||
| 83 | sub value { return( ${$_[0]} ); } | ||
| 84 | |||
| 85 | sub _class_for | ||
| 86 | { | ||
| 87 | my( $self, $mod ) = @_; | ||
| 88 | eval( "require ${BASE_CLASS}\::${mod};" ); | ||
| 89 | die( $@ ) if( $@ ); | ||
| 90 | # ${"${BASE_CLASS}\::${mod}\::DEBUG"} = $DEBUG; | ||
| 91 | eval( "\$${BASE_CLASS}\::${mod}\::DEBUG = " . ( $DEBUG // 0 ) ); | ||
| 92 | return( "${BASE_CLASS}::${mod}" ); | ||
| 93 | } | ||
| 94 | |||
| 95 | 1; | ||
| 96 | # NOTE: POD | ||
| 97 | __END__ | ||
| 98 | |||
| 99 | =encoding utf-8 | ||
| 100 | |||
| 101 | =head1 NAME | ||
| 102 | |||
| 103 | HTML::Object::XPath::Number - HTML Object XPath Number Class | ||
| 104 | |||
| 105 | =head1 SYNOPSIS | ||
| 106 | |||
| 107 | use HTML::Object::XPath::Number; | ||
| 108 | my $num = HTML::Object::XPath::Number->new || | ||
| 109 | die( HTML::Object::XPath::Number->error, "\n" ); | ||
| 110 | |||
| 111 | =head1 VERSION | ||
| 112 | |||
| 113 | v0.2.0 | ||
| 114 | |||
| 115 | =head1 DESCRIPTION | ||
| 116 | |||
| 117 | This module holds simple numeric values. It doesn't support -0, +/- Infinity, or NaN, as the XPath spec says it should. | ||
| 118 | |||
| 119 | =head1 METHODS | ||
| 120 | |||
| 121 | =head2 new | ||
| 122 | |||
| 123 | Provided with a C<number> and this creates a new L<HTML::Object::XPath::Number> object, with the value in C<number>. Does some | ||
| 124 | rudimentary numeric checking on C<number> to ensure it actually is a number. | ||
| 125 | |||
| 126 | =head2 as_string | ||
| 127 | |||
| 128 | Returns a string representation of the number, or C<NaN> is undefined. | ||
| 129 | |||
| 130 | =head2 as_xml | ||
| 131 | |||
| 132 | Same as L</as_string>, but in xml format. | ||
| 133 | |||
| 134 | =head2 cmp | ||
| 135 | |||
| 136 | Returns the equivalent of perl's own L<perlop/cmp> operation. | ||
| 137 | |||
| 138 | =head2 evaluate | ||
| 139 | |||
| 140 | Returns the current object. | ||
| 141 | |||
| 142 | =head2 getAttributes | ||
| 143 | |||
| 144 | Returns an empty array reference in scalar context and an empty list in list context. | ||
| 145 | |||
| 146 | =head2 getChildNodes | ||
| 147 | |||
| 148 | Returns an empty array reference in scalar context and an empty list in list context. | ||
| 149 | |||
| 150 | =head2 new_literal | ||
| 151 | |||
| 152 | Returns a new L<HTML::Object::XPath::Literal> object, passing it whatever arguments was provided. | ||
| 153 | |||
| 154 | =head2 string_value | ||
| 155 | |||
| 156 | Returns the value of the current object by calling L</value> | ||
| 157 | |||
| 158 | =head2 to_boolean | ||
| 159 | |||
| 160 | Returns true if the current object has a L<true|HTML::Object::XPath::Boolean> value, or L<false|HTML::Object::XPath::Boolean> otherwise. | ||
| 161 | |||
| 162 | =head2 to_literal | ||
| 163 | |||
| 164 | Returns a new L<HTML::Object::XPath::Litteral> object of the stringification of the current object. | ||
| 165 | |||
| 166 | =head2 to_number | ||
| 167 | |||
| 168 | Returns the current object. | ||
| 169 | |||
| 170 | =head2 value | ||
| 171 | |||
| 172 | This returns the numeric value held. This is also the method used to return the value from strinfgification. | ||
| 173 | |||
| 174 | =head1 AUTHOR | ||
| 175 | |||
| 176 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||
| 177 | |||
| 178 | =head1 SEE ALSO | ||
| 179 | |||
| 180 | L<HTML::Object::XPath>, L<HTML::Object::XPath::Boolean>, L<HTML::Object::XPath::Expr>, L<HTML::Object::XPath::Function>, L<HTML::Object::XPath::Literal>, L<HTML::Object::XPath::LocationPath>, L<HTML::Object::XPath::NodeSet>, L<HTML::Object::XPath::Number>, L<HTML::Object::XPath::Root>, L<HTML::Object::XPath::Step>, L<HTML::Object::XPath::Variable> | ||
| 181 | |||
| 182 | =head1 COPYRIGHT & LICENSE | ||
| 183 | |||
| 184 | Copyright(c) 2021 DEGUEST Pte. Ltd. | ||
| 185 | |||
| 186 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 187 | |||
| 188 | =cut |