File Coverage

blib/lib/CQL/Visitor.pm
Criterion Covered Total %
statement 13 14 92.8
branch 3 4 75.0
condition 1 3 33.3
subroutine 4 5 80.0
pod 3 3 100.0
total 24 29 82.7


line stmt bran cond sub pod time code
1             package CQL::Visitor;
2              
3 2     2   52046 use strict;
  2         6  
  2         122  
4 2     2   13 use warnings;
  2         5  
  2         414  
5              
6             =head1 NAME
7              
8             CQL::Visitor - visit nodes in a CQL parse tree
9              
10             =head1 SYNOPSIS
11              
12             package MyVisitor;
13             use base qw( CQL::Visitor );
14              
15             sub term {
16             my ($self,$node) = @_;
17             # do something to the node
18             }
19              
20             # later on
21            
22             my $parser = CQL::Parser->new();
23             my $root = $parser->parse($cql);
24              
25             my $visitor = MyVisitor->new();
26             $vistor->visit($root);
27              
28             =head1 DESCRIPTION
29              
30             CQL::Visitor provides a simple interface for visiting nodes in your parse tree.
31             It could be useful if you want to do something like change a query like this:
32              
33             dc.title=foo and dc.creator=bar
34              
35             into
36              
37             title=foo and creator=bar
38              
39             Or some similar procedure. You simply create a new subclass of CQL::Visitor
40             and override the appropriate method, such as term(). Every term that is
41             encountered during the traversal will be handed off to your term() method.
42              
43             Note: at the moment only term() is supported because that's what was needed, but
44             if you need other ones feel free to add them, or ask for them.
45              
46             =head1 METHODS
47              
48             =head2 new()
49              
50             =cut
51              
52             sub new {
53 2     2 1 6 my $class = shift;
54 2   33     16 return bless {}, ref($class) || $class;
55             }
56              
57             =head2 visit()
58              
59             Call this to traverse your parse tree, starting at the root.
60              
61             =cut
62              
63             sub visit {
64 10     10 1 24 my ($self,$node) = @_;
65 10 100       195 if ( $node->isa( 'CQL::BooleanNode' ) ) {
    50          
66 4         13 $self->visit( $node->left() );
67 4         29 $self->visit( $node->right() );
68             }
69             elsif ( $node->isa( 'CQL::TermNode' ) ) {
70 6         17 $self->term( $node );
71             }
72             }
73              
74             =head2 term()
75              
76             Your subclass should override this, and do something meaningful with the
77             CQL::TermNode object.
78              
79             =cut
80              
81 0     0 1   sub term {
82             # subclasses should subclass
83             }
84              
85             1;