| 1 | package Data::DPath::Filters; | ||||||||||
| 2 | 18 | our $AUTHORITY = 'cpan:SCHWIGON'; | |||||||||
| 3 | # ABSTRACT: Magic functions available inside filter conditions | ||||||||||
| 4 | 18 | $Data::DPath::Filters::VERSION = '0.60'; | |||||||||
| 5 | 18 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 18 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 18 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 18 | ▶ | use Scalar::Util; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 18 | ▶ | use constant { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 18 | HASH => 'HASH', | |||||||||
| 12 | ARRAY => 'ARRAY', | ||||||||||
| 13 | SCALAR => 'SCALAR', | ||||||||||
| 14 | 18 | }; | |||||||||
| 15 | |||||||||||
| 16 | 18 | our $idx; | |||||||||
| 17 | 18 | our $p; # current point | |||||||||
| 18 | |||||||||||
| 19 | 14 | ▶ | sub affe { | ||||||||
|
Subroutine
affe: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 20 | 14 | ▶ | return $_ eq 'affe' ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | } | ||||||||||
| 22 | |||||||||||
| 23 | 12 | ▶ | sub idx { $idx } | ||||||||
|
Subroutine
idx: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 24 | |||||||||||
| 25 | sub size() | ||||||||||
| 26 | 292 | ▶ | { | ||||||||
|
Subroutine
size: called CC=8 SCAR=24.0
Pod
documented
| |||||||||||
| 27 | 18 | ▶ | no warnings 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | |||||||||||
| 29 | 292 | ▶ | return -1 unless defined $_; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | # speed optimization: first try faster ref, then reftype | ||||||||||
| 31 | # ref | ||||||||||
| 32 | 292 | ▶ | return scalar @$_ if ref $_ eq ARRAY; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 244 | ▶ | return scalar keys %$_ if ref $_ eq HASH; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 160 | ▶ | return 1 if ref \$_ eq SCALAR; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | # reftype | ||||||||||
| 36 | 6 | ▶ | return scalar @$_ if Scalar::Util::reftype $_ eq ARRAY; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 0 | ▶ | return scalar keys %$_ if Scalar::Util::reftype $_ eq HASH; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 0 | ▶ | return 1 if Scalar::Util::reftype \$_ eq SCALAR; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | # else | ||||||||||
| 40 | 0 | return -1; | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | sub key() | ||||||||||
| 44 | 256 | ▶ | { | ||||||||
|
Subroutine
key: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 45 | 18 | ▶ | no warnings 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 46 | 256 | ▶ | my $attrs = defined $p->attrs ? $p->attrs : {}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 256 | return $attrs->{key}; | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | sub value() | ||||||||||
| 51 | 330 | ▶ | { | ||||||||
|
Subroutine
value: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 52 | 18 | ▶ | no warnings 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 53 | 330 | return $_; | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 12 | ▶ | sub isa($) { | ||||||||
|
Subroutine
isa: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 57 | 12 | my ($classname) = @_; | |||||||||
| 58 | |||||||||||
| 59 | 18 | ▶ | no warnings 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 60 | #print STDERR "*** value ", Dumper($_ ? $_ : "UNDEF"); | ||||||||||
| 61 | 12 | ▶ | return $_->isa($classname) if Scalar::Util::blessed $_; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | 2 | return undef; | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 10 | ▶ | sub reftype() { | ||||||||
|
Subroutine
reftype: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 66 | 10 | return Scalar::Util::reftype($_); | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | 32 | ▶ | sub is_reftype($) { | ||||||||
|
Subroutine
is_reftype: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 70 | 18 | ▶ | no warnings 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 71 | 32 | return (Scalar::Util::reftype($_) eq shift); | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | 18 | 1; | |||||||||
| 75 | |||||||||||
| 76 | =pod | ||||||||||
| 77 | |||||||||||
| 78 | =encoding UTF-8 | ||||||||||
| 79 | |||||||||||
| 80 | =head1 NAME | ||||||||||
| 81 | |||||||||||
| 82 | Data::DPath::Filters - Magic functions available inside filter conditions | ||||||||||
| 83 | |||||||||||
| 84 | =head1 API METHODS | ||||||||||
| 85 | |||||||||||
| 86 | =head2 affe | ||||||||||
| 87 | |||||||||||
| 88 | Mysterious test function. Will vanish. Soon. Or will it really? No, | ||||||||||
| 89 | probably not. I like it. :-) | ||||||||||
| 90 | |||||||||||
| 91 | Returns true if the value eq "affe". | ||||||||||
| 92 | |||||||||||
| 93 | =head2 idx | ||||||||||
| 94 | |||||||||||
| 95 | Returns the current index inside array elements. | ||||||||||
| 96 | |||||||||||
| 97 | Please note that the current matching elements might not be in a | ||||||||||
| 98 | defined order if resulting from anything else than arrays. | ||||||||||
| 99 | |||||||||||
| 100 | =head2 size | ||||||||||
| 101 | |||||||||||
| 102 | Returns the size of the current element. If it is an array ref it | ||||||||||
| 103 | returns the number of elements, if it is a hash ref it returns number of keys, | ||||||||||
| 104 | if it is a scalar it returns 1, everything else returns -1. | ||||||||||
| 105 | |||||||||||
| 106 | =head2 key | ||||||||||
| 107 | |||||||||||
| 108 | If it is a hashref returns the key under which the current element is | ||||||||||
| 109 | associated as value. Else it returns undef. | ||||||||||
| 110 | |||||||||||
| 111 | This gives the key() function kind of a "look back" behaviour because | ||||||||||
| 112 | the associated point is already after that key. | ||||||||||
| 113 | |||||||||||
| 114 | =head2 value | ||||||||||
| 115 | |||||||||||
| 116 | Returns the value of the current element. | ||||||||||
| 117 | |||||||||||
| 118 | =head2 isa | ||||||||||
| 119 | |||||||||||
| 120 | Frontend to UNIVERSAL::isa. True if the current element is_a given | ||||||||||
| 121 | class. | ||||||||||
| 122 | |||||||||||
| 123 | =head2 reftype | ||||||||||
| 124 | |||||||||||
| 125 | Frontend to Scalar::Util::reftype. | ||||||||||
| 126 | |||||||||||
| 127 | Returns Scalar::Util::reftype of current element $_. With this you can | ||||||||||
| 128 | do comparison by yourself with C<eq>, C<=~>, C<~~> or whatever in | ||||||||||
| 129 | filter expressions. | ||||||||||
| 130 | |||||||||||
| 131 | =head2 is_reftype($EXPECTED_TYPE) | ||||||||||
| 132 | |||||||||||
| 133 | Frontend to Scalar::Util::reftype. | ||||||||||
| 134 | |||||||||||
| 135 | Checks whether Scalar::Util::reftype of current element $_ equals the | ||||||||||
| 136 | provided argument $EXPECTED_TYPE and returns true/false. | ||||||||||
| 137 | |||||||||||
| 138 | =head1 AUTHOR | ||||||||||
| 139 | |||||||||||
| 140 | Steffen Schwigon <ss5@renormalist.net> | ||||||||||
| 141 | |||||||||||
| 142 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 143 | |||||||||||
| 144 | This software is copyright (c) 2024 by Steffen Schwigon. | ||||||||||
| 145 | |||||||||||
| 146 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 147 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 148 | |||||||||||
| 149 | =cut | ||||||||||
| 150 | |||||||||||
| 151 | __END__ | ||||||||||