Grove/Path.pm untested

filter: Statementstmt 0.0% 0 / 29 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 46 CC 4 SCAR 22.2
CC
4
Cov
0%
_at_path
20.0
at_path
2.0
at_path
2.0
CRAP 9.2
1 #
2 # Copyright (C) 1998, 1999 Ken MacLeod
3 # XML::Grove::Path is free software; you can redistribute it
4 # and/or modify it under the same terms as Perl itself.
5 #
6 # $Id: Path.pm,v 1.2 1999/08/17 15:01:28 kmacleod Exp $
7 #
8  
9 package XML::Grove::Path;
10  
11 use XML::Grove;
12 use XML::Grove::XPointer;
13 use UNIVERSAL;
14  
15 sub at_path {
16 my $element = shift; # or Grove
17 my $path = shift;
18  
19 $path =~ s|^/*||;
20  
21 my @path = split('/', $path);
22  
23 return (_at_path ($element, [@path]));
24 }
25  
26 sub _at_path {
27 my $element = shift; # or Grove
28 my $path = shift;
29 my $segment = shift @$path;
30  
31 # segment := [ type ] [ '[' index ']' ]
32 #
33 # strip off the first segment, finding the type and index
34 $segment =~ m|^
35 ([^\[]+)? # - look for an optional type
36 # by matching anything but '['
37 (?: # - don't backreference the literals
38 \[ # - literal '['
39 ([^\]]+) # - index, any non-']' chars
40 \] # - literal ']'
41 )? # - the whole index is optional
42 |x;
43 my ($node_type, $instance, $match) = ($1, $2, $&);
44 # issues:
45 # - should assert that no chars come after index and before next
46 # segment or the end of the query string
47  
48 $instance = 1 if !defined $instance;
49  
50 my $object = $element->xp_child ($instance, $node_type);
51  
52 if ($#$path eq -1) {
53 return $object;
54 } elsif (!$object->isa('XML::Grove::Element')) {
55 # FIXME a location would be nice.
56 die "\`$match' doesn't exist or is not an element\n";
57 } else {
58 return (_at_path($object, $path));
59 }
60 }
61  
62 package XML::Grove::Document;
63  
64 sub at_path {
65 goto &XML::Grove::Path::at_path;
66 }
67  
68 package XML::Grove::Element;
69  
70 sub at_path {
71 goto &XML::Grove::Path::at_path;
72 }
73  
74 1;
75  
76 __END__