File Coverage

blib/lib/Data/DPath/Filters.pm
Criterion Covered Total %
statement 31 48 64.5
branch 0 20 0.0
condition n/a
subroutine 11 18 61.1
pod 8 8 100.0
total 50 94 53.1


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.57';
5 12     12   98 use strict;
  12         35  
  12         427  
6 12     12   89 use warnings;
  12         32  
  12         654  
7              
8 12     12   93 use Data::Dumper;
  12         27  
  12         790  
9 12     12   85 use Scalar::Util;
  12         26  
  12         577  
10             use constant {
11 12         2346 HASH => 'HASH',
12             ARRAY => 'ARRAY',
13             SCALAR => 'SCALAR',
14 12     12   82 };
  12         27  
15              
16             our $idx;
17             our $p; # current point
18              
19             sub affe {
20 0 0   0 1 0 return $_ eq 'affe' ? 1 : 0;
21             }
22              
23 0     0 1 0 sub idx { $idx }
24              
25             sub size()
26             {
27 12     12   94 no warnings 'uninitialized';
  12         34  
  12         2495  
28              
29 0 0   0 1 0 return -1 unless defined $_;
30             # speed optimization: first try faster ref, then reftype
31             # ref
32 0 0       0 return scalar @$_ if ref $_ eq ARRAY;
33 0 0       0 return scalar keys %$_ if ref $_ eq HASH;
34 0 0       0 return 1 if ref \$_ eq SCALAR;
35             # reftype
36 0 0       0 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 12     12   133 no warnings 'uninitialized';
  12         37  
  12         1327  
46 0 0   0 1 0 my $attrs = defined $p->attrs ? $p->attrs : {};
47 0         0 return $attrs->{key};
48             }
49              
50             sub value()
51             {
52 12     12   92 no warnings 'uninitialized';
  12         25  
  12         915  
53 111     111 1 830 return $_;
54             }
55              
56             sub isa($) {
57 0     0 1   my ($classname) = @_;
58              
59 12     12   92 no warnings 'uninitialized';
  12         30  
  12         1376  
60             #print STDERR "*** value ", Dumper($_ ? $_ : "UNDEF");
61 0 0         return $_->isa($classname) if Scalar::Util::blessed $_;
62 0           return undef;
63             }
64              
65             sub reftype() {
66 0     0 1   return Scalar::Util::reftype($_);
67             }
68              
69             sub is_reftype($) {
70 12     12   93 no warnings 'uninitialized';
  12         34  
  12         993  
71 0     0 1   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) 2017 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__