| 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 | 12 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 12 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 12 | ▶ | warnings::register_categories( 'HTML::Object' ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | 12 | ▶ | use parent qw( Module::Generic ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | 12 | ▶ | use vars qw( $TRUE $FALSE $BASE_CLASS $DEBUG $VERSION ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 22 | 12 | ▶ | use HTML::Object::XPath::Boolean; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | 12 | our $TRUE = HTML::Object::XPath::Boolean->True; | |||||||||||||||||||
| 24 | 12 | our $FALSE = HTML::Object::XPath::Boolean->False; | |||||||||||||||||||
| 25 | 12 | our $BASE_CLASS = 'HTML::Object::XPath'; | |||||||||||||||||||
| 26 | 12 | our $DEBUG = 0; | |||||||||||||||||||
| 27 | use overload ( | ||||||||||||||||||||
| 28 | 12 | '""' => \&value, | |||||||||||||||||||
| 29 | 'cmp' => \&cmp | ||||||||||||||||||||
| 30 | 12 | ▶ | ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 31 | 12 | our $VERSION = 'v0.2.1'; | |||||||||||||||||||
| 32 | }; | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 12 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 35 | 12 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | sub new | ||||||||||||||||||||
| 38 | { | ||||||||||||||||||||
| 39 | 3976 | ▶ | my $this = shift( @_ ); | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||
| 40 | 3976 | my $str = shift( @_ ); | |||||||||||||||||||
| 41 | 3976 | ▶ | return( bless( \$str => ( ref( $this ) || $this ) ) ); | ||||||||||||||||||
|
Condition33%1/3
ref $this || $this
MC/DC0%0/2
ref $this || $this
ref $this
$this
Truth table33%1/3
A || B
A= ref $thisB= $this
| |||||||||||||||||||||
| 42 | } | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | sub as_string | ||||||||||||||||||||
| 45 | { | ||||||||||||||||||||
| 46 | 0 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||
|
Subroutine
as_string: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 47 | 0 | my $string = $$self; | |||||||||||||||||||
| 48 | 0 | $string =~ s/'/'/g; | |||||||||||||||||||
| 49 | 0 | return( "'$string'" ); | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | sub as_xml | ||||||||||||||||||||
| 53 | { | ||||||||||||||||||||
| 54 | 0 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||
|
Subroutine
as_xml: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 55 | 0 | my $string = $$self; | |||||||||||||||||||
| 56 | 0 | return( "<Literal>$string</Literal>\n" ); | |||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | sub cmp | ||||||||||||||||||||
| 60 | { | ||||||||||||||||||||
| 61 | 0 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||
|
Subroutine
cmp: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 62 | 0 | my( $cmp, $swap ) = @_; | |||||||||||||||||||
| 63 | 0 | ▶ | return( $swap ? $cmp cmp $$self : $$self cmp $cmp ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | sub evaluate | ||||||||||||||||||||
| 67 | { | ||||||||||||||||||||
| 68 | 140 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||
|
Subroutine
evaluate: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 69 | 140 | return( $self ); | |||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 1 | ▶ | sub getChildNodes { die( "cannot get child nodes of a literal" ); } | ||||||||||||||||||
|
Subroutine
getChildNodes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 1 | ▶ | sub getAttributes { die( "cannot get attributes of a literal" ); } | ||||||||||||||||||
|
Subroutine
getAttributes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 1 | ▶ | sub getParentNode { die( "cannot get parent node of a literal" ); } | ||||||||||||||||||
|
Subroutine
getParentNode: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 0 | ▶ | sub new_number { return( shift->_class_for( 'Number' )->new( @_ ) ); } | ||||||||||||||||||
|
Subroutine
new_number: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 57 | ▶ | sub string_value { return( $_[0]->value ); } | ||||||||||||||||||
|
Subroutine
string_value: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | sub to_boolean | ||||||||||||||||||||
| 83 | { | ||||||||||||||||||||
| 84 | 0 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||
|
Subroutine
to_boolean: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 85 | 0 | ▶ | return( ( length( $$self ) > 0 ) ? $TRUE : $FALSE ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 0 | ▶ | sub to_literal { return( $_[0] ); } | ||||||||||||||||||
|
Subroutine
to_literal: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 0 | ▶ | sub to_number { return( $_[0]->new_number( $_[0]->value ) ); } | ||||||||||||||||||
|
Subroutine
to_number: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | sub value | ||||||||||||||||||||
| 93 | { | ||||||||||||||||||||
| 94 | 4072 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||
|
Subroutine
value: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 95 | 4072 | return( $$self ); | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | sub value_as_number | ||||||||||||||||||||
| 99 | { | ||||||||||||||||||||
| 100 | 0 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||
|
Subroutine
value_as_number: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 101 | 0 | ▶ | warn( "numifying '" . $$self . "' to '" . +$$self . "'\n" ) if( $self->_is_warnings_enabled( 'HTML::Object' ) ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | 0 | return( +$$self ); | |||||||||||||||||||
| 103 | } | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | sub _class_for | ||||||||||||||||||||
| 106 | { | ||||||||||||||||||||
| 107 | 0 | ▶ | my( $self, $mod ) = @_; | ||||||||||||||||||
|
Subroutine
_class_for: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 108 | 0 | eval( "require ${BASE_CLASS}\::${mod};" ); | |||||||||||||||||||
| 109 | 0 | ▶ | die( $@ ) if( $@ ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 110 | # ${"${BASE_CLASS}\::${mod}\::DEBUG"} = $DEBUG; | ||||||||||||||||||||
| 111 | 0 | ▶ | eval( "\$${BASE_CLASS}\::${mod}\::DEBUG = " . ( $DEBUG // 0 ) ); | ||||||||||||||||||
|
Condition0%0/2
$HTML::Object::XPath::Literal::DEBUG // 0
MC/DC0%0/1
$HTML::Object::XPath::Literal::DEBUG // 0
$HTML::Object::XPath::Literal::DEBUG
Truth table0%0/2
A
A= $HTML::Object::XPath::Literal::DEBUG
| |||||||||||||||||||||
| 112 | 0 | return( "${BASE_CLASS}::${mod}" ); | |||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 12 | 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 | ||||||||||||||||||||