File Coverage

blib/lib/Text/Query.pm
Criterion Covered Total %
statement 52 67 77.6
branch 15 26 57.6
condition 4 5 80.0
subroutine 10 15 66.6
pod 5 12 41.6
total 86 125 68.8


line stmt bran cond sub pod time code
1             #
2             # Copyright (C) 1999 Eric Bohlman, Loic Dachary
3             # Copyright (C) 2013 Jon Jensen
4             #
5             # This program is free software; you can redistribute it and/or modify it
6             # under the terms of the GNU General Public License as published by the
7             # Free Software Foundation; either version 2, or (at your option) any
8             # later version. You may also use, redistribute and/or modify it
9             # under the terms of the Artistic License supplied with your Perl
10             # distribution
11             #
12             # This program is distributed in the hope that it will be useful,
13             # but WITHOUT ANY WARRANTY; without even the implied warranty of
14             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15             # GNU General Public License for more details.
16             #
17             # You should have received a copy of the GNU General Public License
18             # along with this program; if not, write to the Free Software
19             # Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20              
21             package Text::Query;
22              
23 2     2   12838 use strict;
  2         4  
  2         74  
24              
25 2     2   11 use vars qw($VERSION);
  2         4  
  2         1209  
26              
27             $VERSION = '0.09';
28              
29 2     2   14 use Carp;
  2         8  
  2         17355  
30              
31             sub new {
32 5     5 1 2179 my($class) = shift;
33 5         13 my($self) = {};
34 5         12 bless $self,$class;
35 5 100       26 if(@_ % 2) {
36 4         9 my($qstring) = shift;
37 4         17 $self->configure(@_);
38 4 50       24 return defined($qstring) ? $self->prepare($qstring, @_) : $self;
39             } else {
40 1         6 $self->configure(@_);
41 1         4 return $self;
42             }
43             }
44              
45             sub configure {
46 7     7 1 41 my($self, %args) = @_;
47              
48 7 100 50     69 $self->{-verbose} = $args{-verbose} || 0 if(!defined($self->{-verbose}));
49              
50 7         68 my(%defconfigs) = (
51             simple_text =>
52             { -parse => 'Text::Query::ParseSimple',
53             -build => 'Text::Query::BuildSimpleString',
54             -optimize => 'Text::Query::Optimize',
55             -solve => 'Text::Query::SolveSimpleString'
56             },
57             advanced_text =>
58             { -parse => 'Text::Query::ParseAdvanced',
59             -build => 'Text::Query::BuildAdvancedString',
60             -optimize => 'Text::Query::Optimize',
61             -solve => 'Text::Query::SolveAdvancedString'
62             },
63             );
64              
65 7 50       28 my $default=(defined $args{-mode})?$args{-mode}:'simple_text';
66 7         9 my($key);
67 7         13 foreach $key (keys(%{$defconfigs{$default}})) {
  7         30  
68 28 100       86 my($package) = $args{$key} ? $args{$key} : $defconfigs{$default}{$key};
69 28   100     119 my($load) = !exists($self->{'packages'}{$key}) || $self->{'packages'}{$key} ne $package;
70              
71 28 100       64 if($load) {
72 25         68 $self->{$key} = $self->loader($package);
73 25         181 $self->{$key}->{-verbose} = $self->{-verbose};
74 25 50       75 warn("loaded $package => $self->{$key}") if($self->{-verbose});
75 25         81 $self->{'packages'}{$key} = $package;
76             }
77             }
78 7         68 $self->{-parse}->{-build} = $self->{-build};
79             }
80              
81             sub loader {
82 25     25 0 44 my($self, $package) = @_;
83              
84 25         1546 eval "package Text::Query::_firesafe; require $package";
85              
86 25 50       114 if ($@) {
87 0         0 my($advice) = "";
88 0 0       0 if($@ =~ /Can't find loadable object/) {
    0          
89 0         0 $advice = "Perhaps $package was statically linked into a new perl binary."
90             ."\nIn which case you need to use that new perl binary."
91             ."\nOr perhaps only the .pm file was installed but not the shared object file."
92             } elsif ($@ =~ /Can't locate.*?.pm/) {
93 0         0 $advice = "Perhaps the $package perl module hasn't been installed\n";
94             }
95 0         0 croak("$package failed: $@$advice\n");
96             }
97 25         33 my($object);
98 25         37 $object = eval { $package->new() };
  25         176  
99 25 50       72 croak("$@") if(!defined($object));
100              
101 25         72 return $object;
102             }
103              
104             sub matchexp {
105 0     0 0 0 my($self) = @_;
106              
107 0         0 return $self->{matchexp};
108             }
109              
110             sub matchstring {
111 39     39 0 88 my($self) = @_;
112              
113 39         194 return $self->{-build}->matchstring();
114             }
115              
116             #
117             # Parse interface
118             #
119              
120             sub prepare {
121 43     43 1 108 my($self) = shift;
122              
123 43         224 $self->{matchexp} = $self->{-optimize}->optimize($self->{-parse}->prepare(@_));
124              
125 43         219 return $self;
126             }
127              
128             #
129             # Solve interface
130             #
131              
132             sub match {
133 33     33 1 527 my($self) = shift;
134              
135 33 50       120 croak("solve undefined") if(!$self->{-solve});
136              
137 33         153 return $self->{-solve}->match($self->{matchexp}, @_);
138             }
139              
140             sub matchscalar {
141 2     2 1 3 my($self) = shift;
142              
143 2 50       9 croak("solve undefined") if(!$self->{-solve});
144              
145 2         10 return $self->{-solve}->matchscalar($self->{matchexp}, @_);
146             }
147              
148             #
149             # Accessors
150             #
151              
152             sub build {
153 0     0 0   my($self) = shift;
154 0           return $self->{-build};
155             }
156              
157             sub parse {
158 0     0 0   my($self) = shift;
159 0           return $self->{-parse};
160             }
161              
162             sub solve {
163 0     0 0   my($self) = shift;
164 0           return $self->{-solve};
165             }
166              
167             sub optimize {
168 0     0 0   my($self) = shift;
169 0           return $self->{-optimize};
170             }
171              
172             1;
173              
174             __END__