| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## HTML Object - ~/lib/HTML/Object/XPath/Literal.pm | ||
| 3 | ## Version v0.2.1 | ||
| 4 | ## Copyright(c) 2022 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2021/12/05 | ||
| 7 | ## Modified 2022/11/11 | ||
| 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::Literal; | ||
| 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 | use overload ( | ||
| 28 | '""' => \&value, | ||
| 29 | 'cmp' => \&cmp | ||
| 30 | ); | ||
| 31 | our $VERSION = 'v0.2.1'; | ||
| 32 | }; | ||
| 33 | |||
| 34 | use strict; | ||
| 35 | use warnings; | ||
| 36 | |||
| 37 | sub new | ||
| 38 | { | ||
| 39 | my $this = shift( @_ ); | ||
| 40 | my $str = shift( @_ ); | ||
| 41 | return( bless( \$str => ( ref( $this ) || $this ) ) ); | ||
| 42 | } | ||
| 43 | |||
| 44 | sub as_string | ||
| 45 | { | ||
| 46 | my $self = shift( @_ ); | ||
| 47 | my $string = $$self; | ||
| 48 | $string =~ s/'/'/g; | ||
| 49 | return( "'$string'" ); | ||
| 50 | } | ||
| 51 | |||
| 52 | sub as_xml | ||
| 53 | { | ||
| 54 | my $self = shift( @_ ); | ||
| 55 | my $string = $$self; | ||
| 56 | return( "<Literal>$string</Literal>\n" ); | ||
| 57 | } | ||
| 58 | |||
| 59 | sub cmp | ||
| 60 | { | ||
| 61 | my $self = shift( @_ ); | ||
| 62 | my( $cmp, $swap ) = @_; | ||
| 63 | return( $swap ? $cmp cmp $$self : $$self cmp $cmp ); | ||
| 64 | } | ||
| 65 | |||
| 66 | sub evaluate | ||
| 67 | { | ||
| 68 | my $self = shift( @_ ); | ||
| 69 | return( $self ); | ||
| 70 | } | ||
| 71 | |||
| 72 | sub getChildNodes { die( "cannot get child nodes of a literal" ); } | ||
| 73 | |||
| 74 | sub getAttributes { die( "cannot get attributes of a literal" ); } | ||
| 75 | |||
| 76 | sub getParentNode { die( "cannot get parent node of a literal" ); } | ||
| 77 | |||
| 78 | sub new_number { return( shift->_class_for( 'Number' )->new( @_ ) ); } | ||
| 79 | |||
| 80 | sub string_value { return( $_[0]->value ); } | ||
| 81 | |||
| 82 | sub to_boolean | ||
| 83 | { | ||
| 84 | my $self = shift( @_ ); | ||
| 85 | return( ( length( $$self ) > 0 ) ? $TRUE : $FALSE ); | ||
| 86 | } | ||
| 87 | |||
| 88 | sub to_literal { return( $_[0] ); } | ||
| 89 | |||
| 90 | sub to_number { return( $_[0]->new_number( $_[0]->value ) ); } | ||
| 91 | |||
| 92 | sub value | ||
| 93 | { | ||
| 94 | my $self = shift( @_ ); | ||
| 95 | return( $$self ); | ||
| 96 | } | ||
| 97 | |||
| 98 | sub value_as_number | ||
| 99 | { | ||
| 100 | my $self = shift( @_ ); | ||
| 101 | warn( "numifying '" . $$self . "' to '" . +$$self . "'\n" ) if( $self->_is_warnings_enabled( 'HTML::Object' ) ); | ||
| 102 | return( +$$self ); | ||
| 103 | } | ||
| 104 | |||
| 105 | sub _class_for | ||
| 106 | { | ||
| 107 | my( $self, $mod ) = @_; | ||
| 108 | eval( "require ${BASE_CLASS}\::${mod};" ); | ||
| 109 | die( $@ ) if( $@ ); | ||
| 110 | # ${"${BASE_CLASS}\::${mod}\::DEBUG"} = $DEBUG; | ||
| 111 | eval( "\$${BASE_CLASS}\::${mod}\::DEBUG = " . ( $DEBUG // 0 ) ); | ||
| 112 | return( "${BASE_CLASS}::${mod}" ); | ||
| 113 | } | ||
| 114 | |||
| 115 | 1; | ||
| 116 | # NOTE: POD | ||
| 117 | __END__ | ||
| 118 | |||
| 119 | =encoding utf-8 | ||
| 120 | |||
| 121 | =head1 NAME | ||
| 122 | |||
| 123 | HTML::Object::XPath::Literal - HTML Object XPath Literal | ||
| 124 | |||
| 125 | =head1 SYNOPSIS | ||
| 126 | |||
| 127 | use HTML::Object::XPath::Literal; | ||
| 128 | my $this = HTML::Object::XPath::Literal->new || | ||
| 129 | die( HTML::Object::XPath::Literal->error, "\n" ); | ||
| 130 | |||
| 131 | =head1 VERSION | ||
| 132 | |||
| 133 | v0.2.1 | ||
| 134 | |||
| 135 | =head1 DESCRIPTION | ||
| 136 | |||
| 137 | This module implements the equivalent of a string in XPath parlance. | ||
| 138 | |||
| 139 | =head1 METHODS | ||
| 140 | |||
| 141 | =head2 new | ||
| 142 | |||
| 143 | Provided with a C<string> and this creates a new L<HTML::Object::XPath::Literal> object with the value providedd. Note that " and | ||
| 144 | ' will be converted to " and ' respectively. That is not part of the XPath specification, but I consider it useful. Note though that you have to go to extraordinary lengths in an XML template file (be it XSLT or whatever) to | ||
| 145 | make use of this: | ||
| 146 | |||
| 147 | <input type="text" value=""I am feeling &quot;perplex&quot;"" /> | ||
| 148 | |||
| 149 | Which produces a Literal of: | ||
| 150 | |||
| 151 | I am feeling "perplex" | ||
| 152 | |||
| 153 | =head2 as_string | ||
| 154 | |||
| 155 | Returns a string representation of the literal. | ||
| 156 | |||
| 157 | =head2 as_xml | ||
| 158 | |||
| 159 | Returns a string representation of the literal as xml. | ||
| 160 | |||
| 161 | =head2 cmp | ||
| 162 | |||
| 163 | This is a method used for overload. Provided with another object or string and this will return the same value as L<perlop/cmp>, that is it "returns -1, 0, or 1 depending on whether the left argument is stringwise less than, equal to, or greater than the right argument". | ||
| 164 | |||
| 165 | =head2 evaluate | ||
| 166 | |||
| 167 | It returns the current object. | ||
| 168 | |||
| 169 | =head2 getChildNodes | ||
| 170 | |||
| 171 | This raises an exception, as it cannot be used. | ||
| 172 | |||
| 173 | =head2 getAttributes | ||
| 174 | |||
| 175 | This raises an exception, as it cannot be used. | ||
| 176 | |||
| 177 | =head2 getParentNode | ||
| 178 | |||
| 179 | This raises an exception, as it cannot be used. | ||
| 180 | |||
| 181 | =head2 new_number | ||
| 182 | |||
| 183 | Returns a new L<number object|HTML::Object::XPath::Number> based on the value provided. | ||
| 184 | |||
| 185 | =head2 string_value | ||
| 186 | |||
| 187 | Returns the value of the literal as returned by L</value> | ||
| 188 | |||
| 189 | =head2 to_boolean | ||
| 190 | |||
| 191 | Returns L<true|HTML::Object::XPath::Boolean> if the literal value is true, or L<false|HTML::Object::XPath::Boolean> otherwise. | ||
| 192 | |||
| 193 | =head2 to_literal | ||
| 194 | |||
| 195 | Returns the current object. | ||
| 196 | |||
| 197 | =head2 to_number | ||
| 198 | |||
| 199 | Returns a new L<number object|HTML::Object::XPath::Number> from the value of the literal. | ||
| 200 | |||
| 201 | =head2 value | ||
| 202 | |||
| 203 | This returns the literal string value. It is also called upon stringification. | ||
| 204 | |||
| 205 | =head2 value_as_number | ||
| 206 | |||
| 207 | Returns the literal value as a number (not a number object), but forcing perl to treat it as a number, i.e. prepending it with a plus sign. | ||
| 208 | |||
| 209 | =head1 AUTHOR | ||
| 210 | |||
| 211 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||
| 212 | |||
| 213 | =head1 SEE ALSO | ||
| 214 | |||
| 215 | 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> | ||
| 216 | |||
| 217 | =head1 COPYRIGHT & LICENSE | ||
| 218 | |||
| 219 | Copyright(c) 2021 DEGUEST Pte. Ltd. | ||
| 220 | |||
| 221 | All rights reserved | ||
| 222 | |||
| 223 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 224 | |||
| 225 | =cut |