blib/lib/Geo/Location/Predicate.pm

filter: Statementstmt 100% 14 / 14 Branchbran 100% 2 / 2 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 100% 29 / 29 CC 4 SCAR 10.0
CC
4
Cov
100%
_valid_location
4.0
BEGIN
1.0
BEGIN
1.0
CRAP 2.7
↑ Summary
1 package Geo::Location::Predicate;
2  
3 # The one definition of what counts as a location.
4 #
5 # It was a module of Geo::Coder::Free until 0.48, and lives in a distribution of
6 # its own now for two reasons. The first is weight: the six geocoders that ask
7 # the rule - Geo::Coder::CA, DataScienceToolkit, GeoApify, List, Postcodes and
8 # XYZ - are releases of their own, and while the rule lived inside
9 # Geo::Coder::Free the only way for them to name it in their metadata was to
10 # require the facade too, with its backends, its databases and its cache; so each
11 # looked for the module when it loaded instead of declaring it. The second is
12 # that the rule is useful on its own and does not need any of that: it is a cheap
13 # syntactic test with no dependencies at all, so it can be the dependency the six
14 # declare - a recommendation in their META, which an installer fetches by
15 # default.
16 #
17 # The name says the question, not the namespace it was written in. It is a
18 # predicate - could this string be a location? - and not a validator, which
19 # would promise that the string is a correct address or a real place; the answer
20 # is a policy about whether a lookup is worth making, so a numeric geocoder may
21 # want it to say true where this says false. Geo::Location::* is where a
22 # location is looked at rather than where one is looked up (that is Geo::Coder::*,
23 # ::CA and ::OSM being geocoders for a service each), and it sits beside
24 # Geo::Location::Point for a caller who already has coordinates in hand. Every
25 # caller says Geo::Location::Predicate::_valid_location(), including the facade
26 # and both of its backends; the sub keeps its private name so that it does not
27 # compete with the public spelling the facade already offers.
28 #
29 # The class - not the file - is the unit: L<Geo::Coder::Free> calls this rule and
30 # depends on this distribution, and the six call it when it is installed and fall
31 # back to refusing a reference when it is not.
32 #
33 # Zero dependencies is the point of the distribution, so nothing beyond core
34 # perl is loaded here - not Params::Validate::Strict, not Readonly, not
35 # Sub::Private. Each of those would become a prerequisite of eight
36 # distributions to guard one line, and Sub::Private would stop the eight
37 # callers from calling the rule at all.
38  
39 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 # Exporter's import() is inherited by name rather than through @ISA; that form
43 # needs Exporter 5.57, which is first in perl 5.8.3 (hence MIN_PERL_VERSION).
44 3 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
45 3 our @EXPORT_OK = qw(_valid_location);
46  
47 =head1 NAME
48  
49 Geo::Location::Predicate - The rule that decides whether a string could be a location
50  
51 =head1 VERSION
52  
53 Version 0.02
54  
55 =cut
56  
57 3 our $VERSION = '0.02';
58  
59 =head1 SYNOPSIS
60  
61 Ask the question before you spend time on a lookup:
62  
63 use Geo::Location::Predicate qw(_valid_location);
64  
65 if(_valid_location($row->{'place'})) {
66 # The value could be a place, so it is worth a lookup
67 }
68  
69 Remove the rows that cannot be places, before a batch of lookups:
70  
71 use Geo::Location::Predicate qw(_valid_location);
72  
73 my @to_look_up = grep { _valid_location($_->{'place'}) } @rows;
74  
75 Call the rule by its full name, without importing anything:
76  
77 require Geo::Location::Predicate;
78  
79 my $ok = Geo::Location::Predicate::_valid_location($text);
80  
81 Use the rule only when it is installed, and fall back to a weaker test when it
82 is not. This is what the six sibling geocoders do:
83  
84 my $is_location = eval { require Geo::Location::Predicate; 1 }
85 ? \&Geo::Location::Predicate::_valid_location
86 : sub { defined($_[0]) && !ref($_[0]) && length($_[0]) };
87  
88 return unless $is_location->($location);
89  
90 Clean the value first, if it comes from a file:
91  
92 chomp(my $place = <$fh>);
93 $place =~ s/^\s+|\s+$//g; # remove spaces at the start and the end
94 next unless _valid_location($place);
95  
96 =head1 DESCRIPTION
97  
98 This module answers one question: B<could this value be a location?>
99  
100 The answer is "no" (C<0>) for:
101  
102 =over 4
103  
104 =item * C<undef> (no value)
105  
106 =item * the empty string C<''>
107  
108 =item * a reference of any kind (an array, a hash, an object, ...)
109  
110 =item * a string that contains only digits, for example C<'0044'>
111  
112 =back
113  
114 The answer is "yes" (C<1>) for every other value. The value needs only one
115 character that is not a digit. So C<'London'>, C<'Ramsgate, Kent'>, C<'3.14'>,
116 C<'-'> and even C<' '> (one space) all get "yes".
117  
118 =head2 Why this rule exists
119  
120 A geocoder often reads a table, such as a CSV file. When a program reads the
121 wrong column by mistake, it gets numbers (an ID, a phone number, a count)
122 instead of place names. A number made only of digits is never a place name,
123 so a lookup for it wastes time and may return a wrong match. This rule stops
124 those lookups.
125  
126 =head2 What "yes" means
127  
128 "Yes" does B<not> mean that the place exists. It means only that the value is
129 not obviously wrong. C<'Ramsgate, Kent'> gets "yes", and a geocoder may still
130 find nothing for it. The test only looks at the characters. It does not use a
131 database or the network, so it is very fast.
132  
133 =head2 Who uses it
134  
135 L<Geo::Coder::Free> asks this question before it searches. Its two backends,
136 L<Geo::Coder::Free::MaxMind> and L<Geo::Coder::Free::OpenAddresses>, ask it too.
137 Six other geocoders also ask it: Geo::Coder::CA, Geo::Coder::DataScienceToolkit,
138 Geo::Coder::GeoApify, Geo::Coder::List, Geo::Coder::Postcodes and
139 Geo::Coder::XYZ.
140  
141 All of them use this one copy of the rule. This is on purpose: if each one had
142 its own copy, the copies could change in different ways, and one program could
143 accept a value that another refuses.
144  
145 L<Geo::Coder::Free> needs this module. For the six other geocoders it is
146 optional: they list it as "recommended", and they load it only when they first
147 need it. Without it, they can still refuse a reference, but they cannot refuse
148 a string of digits.
149  
150 This module has no dependencies outside core Perl. That is why it is a
151 separate distribution: the six geocoders can recommend it without also
152 installing Geo::Coder::Free and its databases.
153  
154 =head2 Should I call it?
155  
156 Usually not. If you use L<Geo::Coder::Free>, call its public method
157 C<< $geo->valid_location($place) >>, which gives the same answer. The name of
158 this function starts with C<_> because it is meant for the geocoders listed
159 above. But the name will not change, because those geocoders depend on it.
160  
161 =head1 SUBROUTINES/METHODS
162  
163 =head2 _valid_location
164  
165 my $answer = _valid_location($value);
166  
167 =over 4
168  
169 =item Purpose
170  
171 Says whether a value could be a location, so that the caller can skip a lookup
172 that cannot work.
173  
174 =item Arguments
175  
176 One value of any kind. If you give more than one argument, only the first is
177 used. If you give no argument, it is treated as C<undef>.
178  
179 =item Returns
180  
181 The number C<1> ("yes, it could be a location") if the value is a plain string
182 or number and has at least one character that is not a digit.
183  
184 The number C<0> ("no") in every other case.
185  
186 It always returns exactly one value, also in list context. So it is safe to
187 use inside C<map>.
188  
189 =item Side effects
190  
191 None. It does not die and does not warn, whatever you give it. It does not
192 change its argument, C<$_>, C<$!>, C<$@>, or the match variables (C<$1> and so
193 on) of the caller. It does not call the stringify (C<"">) overload of an
194 object.
195  
196 =item Usage
197  
198 Call it as a plain function, not as a method. There is no object and no
199 C<new()>. Import it by name, because it is not exported by default:
200  
201 use Geo::Location::Predicate qw(_valid_location);
202  
203 =back
204  
205 =head3 EXAMPLE
206  
207 use Geo::Location::Predicate qw(_valid_location);
208  
209 _valid_location('Ramsgate, Kent'); # 1 - a place name
210 _valid_location('GB'); # 1 - a country code
211 _valid_location('3.14'); # 1 - the '.' is not a digit
212 _valid_location(' '); # 1 - a space is not a digit
213 _valid_location('0044'); # 0 - only digits
214 _valid_location(44); # 0 - a number is read as '44'
215 _valid_location(-44); # 1 - a number is read as '-44'
216 _valid_location(''); # 0 - empty
217 _valid_location(undef); # 0 - no value
218 _valid_location(['London']); # 0 - a reference
219  
220 # Keep only the rows that could be places
221 my @to_look_up = grep { _valid_location($_->{'place'}) } @rows;
222  
223 =head3 API SPECIFICATION
224  
225 =head4 INPUT
226  
227 One positional argument, written in L<Params::Validate::Strict> form. Every
228 value is accepted, so validation never fails: the job of this function is to
229 answer "no" for a bad value, not to reject it.
230  
231 [
232 {
233 name => 'location',
234 type => 'any', # a string, a number, undef or a reference
235 optional => 1, # no argument means undef
236 },
237 ]
238  
239 =head4 OUTPUT
240  
241 Written in L<Return::Set> form:
242  
243 {
244 type => 'integer',
245 min => 0,
246 max => 1,
247 }
248  
249 =head3 MESSAGES
250  
251 The function itself never gives an error or a warning. The only messages come
252 from Perl when the module is loaded in a wrong way:
253  
254 +------------------------------------+----------------------+----------------------+
255 | Message | Meaning | What to do |
256 +------------------------------------+----------------------+----------------------+
257 | "NAME" is not exported by the | You asked to import | Import only |
258 | Geo::Location::Predicate module | a name that does not | _valid_location |
259 | Can't continue after import errors | exist, for example | (with the leading |
260 | | valid_location | underscore) |
261 +------------------------------------+----------------------+----------------------+
262 | Geo::Location::Predicate version N | You asked for a | Install a newer |
263 | required--this is only version M | newer version than | version, or ask for |
264 | | the one installed | a lower one |
265 +------------------------------------+----------------------+----------------------+
266 | Can't locate | The module is not | Install it, or load |
267 | Geo/Location/Predicate.pm in @INC | installed | it inside eval {} as |
268 | | | in the SYNOPSIS |
269 +------------------------------------+----------------------+----------------------+
270 | Undefined subroutine | You called the | Import it by name, |
271 | &main::_valid_location called | function without | or use its full name |
272 | | importing it | |
273 +------------------------------------+----------------------+----------------------+
274  
275 =cut
276  
277 # One expression, one exit: the three disqualifications are tested in order of
278 # cost, and each short-circuits the next. ref() comes before the match so that
279 # an object is never stringified - its "" overload is not called and cannot die
280 # or have side effects. No length() test is needed: '' has no non-digit, so the
281 # match already refuses it.
282 80 ▶sub _valid_location {
Subroutine _valid_location: called CC=4 SCAR=13.9
283 80 my $location = shift;
284  
285 80 ▶ return (defined($location) && !ref($location) && $location =~ /\D/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $location && !ref($location) && $location =~ /\D/ ? : 51 29 80
Condition100%6/6
defined $location and not ref $location and $location =~ /\D/
!ll&&!rl&&r
92051
defined $location and not ref $location
!ll&&!rl&&r
4571
MC/DC100%3/3
defined $location and not ref $location and $location =~ /\D/
defined $location not ref $location $location =~ /\D/
Truth table100%4/4
A and not(B) and C
A= defined $locationB= not ref $locationC= $location =~ /\D/
ABCresult
0XX0
10X0
1100
1111
286 }
287  
288 3 1;
289  
290 __END__
↑ Summary