File Coverage

blib/lib/Data/DPath/Filters.pm
Criterion Covered Total %
statement 45 48 93.7
branch 14 20 70.0
condition n/a
subroutine 18 18 100.0
pod 8 8 100.0
total 85 94 90.4


line stmt bran cond sub pod time code
1             package Data::DPath::Filters;
2             our $AUTHORITY = 'cpan:SCHWIGON';
3             # ABSTRACT: Magic functions available inside filter conditions
4             $Data::DPath::Filters::VERSION = '0.58';
5 13     13   78 use strict;
  13         25  
  13         332  
6 13     13   62 use warnings;
  13         38  
  13         297  
7              
8 13     13   56 use Data::Dumper;
  13         22  
  13         465  
9 13     13   71 use Scalar::Util;
  13         37  
  13         588  
10             use constant {
11 13         2060 HASH => 'HASH',
12             ARRAY => 'ARRAY',
13             SCALAR => 'SCALAR',
14 13     13   85 };
  13         25  
15              
16             our $idx;
17             our $p; # current point
18              
19             sub affe {
20 14 100   14 1 3499 return $_ eq 'affe' ? 1 : 0;
21             }
22              
23 12     12 1 2991 sub idx { $idx }
24              
25             sub size()
26             {
27 13     13   96 no warnings 'uninitialized';
  13         24  
  13         2403  
28              
29 292 50   292 1 68608 return -1 unless defined $_;
30             # speed optimization: first try faster ref, then reftype
31             # ref
32 292 100       1397 return scalar @$_ if ref $_ eq ARRAY;
33 244 100       1522 return scalar keys %$_ if ref $_ eq HASH;
34 160 100       2022 return 1 if ref \$_ eq SCALAR;
35             # reftype
36 6 50       113 return scalar @$_ if Scalar::Util::reftype $_ eq ARRAY;
37 0 0       0 return scalar keys %$_ if Scalar::Util::reftype $_ eq HASH;
38 0 0       0 return 1 if Scalar::Util::reftype \$_ eq SCALAR;
39             # else
40 0         0 return -1;
41             }
42              
43             sub key()
44             {
45 13     13   89 no warnings 'uninitialized';
  13         25  
  13         1191  
46 256 100   256 1 63794 my $attrs = defined $p->attrs ? $p->attrs : {};
47 256         3593 return $attrs->{key};
48             }
49              
50             sub value()
51             {
52 13     13   89 no warnings 'uninitialized';
  13         89  
  13         992  
53 327     327 1 51202 return $_;
54             }
55              
56             sub isa($) {
57 12     12 1 3344 my ($classname) = @_;
58              
59 13     13   84 no warnings 'uninitialized';
  13         33  
  13         1408  
60             #print STDERR "*** value ", Dumper($_ ? $_ : "UNDEF");
61 12 100       220 return $_->isa($classname) if Scalar::Util::blessed $_;
62 2         24 return undef;
63             }
64              
65             sub reftype() {
66 10     10 1 2866 return Scalar::Util::reftype($_);
67             }
68              
69             sub is_reftype($) {
70 13     13   82 no warnings 'uninitialized';
  13         27  
  13         966  
71 32     32 1 8168 return (Scalar::Util::reftype($_) eq shift);
72             }
73              
74             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, 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
141              
142             =head1 COPYRIGHT AND LICENSE
143              
144             This software is copyright (c) 2019 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__