File Coverage

blib/lib/Data/ZPath.pm
Criterion Covered Total %
statement 66 66 100.0
branch 10 12 83.3
condition 4 6 66.6
subroutine 17 17 100.0
pod 6 6 100.0
total 103 107 96.2


line stmt bran cond sub pod time code
1 8     8   1603831 use strict;
  8         18  
  8         354  
2 8     8   57 use warnings;
  8         15  
  8         879  
3              
4             package Data::ZPath;
5              
6 8     8   58 use Carp qw(croak);
  8         33  
  8         633  
7              
8 8     8   4582 use Data::ZPath::_Ctx;
  8         27  
  8         338  
9 8     8   4881 use Data::ZPath::_Lexer;
  8         30  
  8         349  
10 8     8   63 use Data::ZPath::Node;
  8         17  
  8         244  
11 8     8   3893 use Data::ZPath::NodeList;
  8         29  
  8         314  
12 8     8   5530 use Data::ZPath::_Parser;
  8         30  
  8         348  
13 8     8   6349 use Data::ZPath::_ScalarProxy;
  8         25  
  8         281  
14 8     8   4746 use Data::ZPath::_Evaluate;
  8         32  
  8         954  
15              
16             our $DEBUG = 0;
17              
18             our $VERSION = '0.001000';
19              
20             our @CARP_NOT = qw(
21             Data::ZPath::_Ctx
22             Data::ZPath::_Lexer
23             Data::ZPath::Node
24             Data::ZPath::NodeList
25             Data::ZPath::_Parser
26             Data::ZPath::_ScalarProxy
27             Data::ZPath::_Evaluate
28             );
29              
30             for my $pkg ( @CARP_NOT ) {
31 8     8   84 no strict 'refs';
  8         33  
  8         8157  
32             *{"${pkg}::CARP_NOT"} = \@CARP_NOT;
33             }
34              
35             our $Epsilon = 1e-08;
36             our $UseBigInt = !!1;
37             our $XmlIgnoreWS = !!1;
38              
39             sub new {
40 377     377 1 1893204 my ( $class, $expr ) = @_;
41 377 100       1415 croak "Missing expression" unless defined $expr;
42              
43 376         1537 my $self = bless {
44             expr_src => $expr,
45             terms => Data::ZPath::_Parser::_parse_top_level_terms($expr),
46             }, $class;
47              
48 367         1396 return $self;
49             }
50              
51             sub evaluate {
52 375     375 1 2998 my ( $self, $root, %opts ) = @_;
53 375         693 my $wantarray = wantarray;
54              
55 375         1769 my $ctx = Data::ZPath::_Ctx->new($root);
56 375         657 my @out;
57              
58 375         552 for my $term (@{$self->{terms}}) {
  375         1076  
59 431         1419 push @out, Data::ZPath::_Evaluate::_eval_expr($term, $ctx);
60 431 100 100     1650 last if ( $opts{first} and @out );
61             }
62              
63 375         1776 return Data::ZPath::NodeList->_new_or_list(@out);
64             }
65              
66             sub all {
67 15     15 1 788 my ( $self, $root ) = @_;
68 15         48 map $_->value, $self->evaluate( $root )->all;
69             }
70              
71             sub first {
72 5     5 1 27 my ( $self, $root ) = @_;
73 5 100       18 my $found = $self->evaluate($root, first => 1)->first
74             or return undef;
75 4         21 return $found->value;
76             }
77              
78             sub last {
79 1     1 1 8 my ( $self, $root ) = @_;
80 1 50       10 my $found = $self->evaluate($root)->last
81             or return undef;
82 1         8 return $found->value;
83             }
84              
85             sub each {
86 3     3 1 589 my ( $self, $root, $cb ) = @_;
87 3 100       239 croak "each() requires a coderef" unless ref($cb) eq 'CODE';
88              
89 2         10 my $ctx = Data::ZPath::_Ctx->new($root);
90 2         4 for my $term (@{$self->{terms}}) {
  2         7  
91 2         7 my @res = Data::ZPath::_Evaluate::_eval_expr($term, $ctx);
92              
93 2         7 for my $node (@res) {
94 2         25 my $slot = $node->slot;
95 2 50 33     14 croak "each() can only mutate Perl map/list scalars (not XML)" unless $slot && ref($slot) eq 'CODE';
96              
97 2         18 tie my $proxy, 'Data::ZPath::_ScalarProxy', $slot;
98 2         8 $cb->() for $proxy;
99             }
100             }
101              
102 2         10 return;
103             }
104              
105             1;
106              
107             __END__