File Coverage

blib/lib/Data/FR/Town.pm
Criterion Covered Total %
statement 51 55 92.7
branch 10 10 100.0
condition n/a
subroutine 11 13 84.6
pod 9 9 100.0
total 81 87 93.1


line stmt bran cond sub pod time code
1             package Data::FR::Town;
2              
3 2     2   131528 use 5.006;
  2         10  
  2         130  
4 2     2   84 use strict;
  2         4  
  2         84  
5 2     2   11 use warnings;
  2         9  
  2         86  
6 2     2   11 use Carp;
  2         6  
  2         1750  
7              
8             =head1 NAME
9              
10             Data::FR::Town - Provides data about french town (INSEE code, ZIP/postal code, name...)
11              
12             =head1 VERSION
13              
14             Version 0.01
15              
16             =cut
17              
18             our $VERSION = '0.02';
19              
20             our %TOWNS;
21              
22             =head1 SYNOPSIS
23              
24             This module provides basic data about french towns.
25              
26             use Data::FR::Town;
27              
28             # Multiple ways to get the same town
29             my $town1 = Data::FR::Town->new( {insee => "01001"});
30             my $town2 = Data::FR::Town->new( {zip => "01400"});
31             my $town3 = Data::FR::Town->find( {insee => "01001"});
32             my $town3 = Data::FR::Town->find( {zip => "01400"});
33              
34             Now you can get data about the selected town, using the getters :
35              
36             my $zip = $town1->zip();
37             my $name = $town1->name();
38             my $cname = $town1->cname();
39             ...
40              
41             =head1 SUBROUTINES/METHODS
42              
43             =head2 new ( \%params )
44              
45             Return a town object selected through the specified parameters.
46              
47             %params accept two valid keys : 'insee' and 'zip' to specfy the town.
48              
49             =cut
50              
51             sub new {
52 4     4 1 4236 my $class = shift;
53 4         8 my $params = shift;
54              
55 4         15 my $self = bless {}, $class;
56              
57 4         24402 for my $line () {
58 36340         53159 chomp $line;
59              
60 36340         213572 my ( $insee, $article, $name, $cname, $zip, $dep, $depname, $origin ) = split ';', $line;
61              
62 36340         198145 $class::TOWNS{insee}{$insee}{article} = $article;
63 36340         78945 $class::TOWNS{insee}{$insee}{name} = $name;
64 36340         97726 $class::TOWNS{insee}{$insee}{cname} = $cname;
65 36340         79030 $class::TOWNS{insee}{$insee}{zip} = $zip;
66 36340         79818 $class::TOWNS{insee}{$insee}{dep} = $dep;
67 36340         67675 $class::TOWNS{insee}{$insee}{depname} = $depname;
68 36340         94916 $class::TOWNS{insee}{$insee}{origin} = $origin;
69 36340         158755 $class::TOWNS{zip}{$zip} = $class::TOWNS->{insee}{$insee};
70              
71             }
72              
73 4         7151 for my $param ( keys %{$params} ) {
  4         28  
74 3 100       51 if ( $param =~ /insee/ ) {
    100          
75 1         8 return $class->find( { insee => $params->{$param} } );
76             } elsif ( $param =~ /zip/ ) {
77 1         8 return $class->find( { zip => $params->{$param} } );
78              
79             } else {
80 1         335 croak "Unknown parameter : $param";
81             }
82             }
83              
84 1         23 return bless $self, $class;
85             }
86              
87             =head2 find ( \%params )
88              
89             Return a town object selected through the specified parameters.
90              
91             %params accept two valid keys : 'insee' and 'zip' to specfy the town.
92              
93             =cut
94              
95             sub find {
96 5     5 1 9040 my $class = shift;
97 5         9 my $params = shift;
98              
99 5 100       24 $class = ref $class if ref $class;
100              
101 5         23 for my $param ( keys %$params ) {
102 5 100       29 if ( $class::TOWNS{$param}{ $params->{$param} } ) {
103 3         25 return bless $class::TOWNS{$param}{ $params->{$param} }, $class;
104             } else {
105 2 100       120 croak "Unknown parameter : $param" unless $param =~ /(zip|insee)/;
106 1         6 return undef;
107             }
108             }
109             }
110              
111             =head2 insee ()
112              
113             Return the INSEE code associated to this town
114              
115             =cut
116              
117             sub insee {
118 0     0 1 0 my $self = shift;
119              
120 0         0 return $self->{insee};
121             }
122              
123             =head2 zip ()
124              
125             Return the zip/postal code associated to this town
126              
127             =cut
128              
129             sub zip {
130 2     2 1 6 my $self = shift;
131              
132 2         14 return $self->{zip};
133             }
134              
135             =head2 article ()
136              
137             Return the article associated to this town, as some french town may have one.
138             (La Courneuve)
139              
140             =cut
141              
142             sub article {
143 2     2 1 4 my $self = shift;
144              
145 2         13 return $self->{article};
146             }
147              
148             =head2 name ()
149              
150             Return the name of the town
151             The first letter is uppercased the other ones are lowercase
152              
153             =cut
154              
155             sub name {
156 0     0 1 0 my $self = shift;
157              
158 0         0 return $self->{name};
159             }
160              
161             =head2 cname ()
162              
163             Return the capitalized name of the town
164             All letters are (non accentuated) uppercase.
165              
166             =cut
167              
168             sub cname {
169 2     2 1 1329 my $self = shift;
170              
171 2         52 return $self->{cname};
172             }
173              
174             =head2 dep ()
175              
176             Return the department's number
177              
178             =cut
179              
180             sub dep {
181 2     2 1 6 my $self = shift;
182              
183 2         11 return $self->{dep};
184             }
185              
186             =head2 depname ()
187              
188             Return the department's name
189              
190             =cut
191              
192             sub depname {
193 2     2 1 5 my $self = shift;
194              
195 2         11 return $self->{depname};
196             }
197              
198             =head1 DISCLAIMER
199              
200             This version is based on the data from the INSEE site :
201             http://www.insee.fr/fr/methodes/nomenclatures/cog/telechargement/2012/txt/comsimp2012.zip
202              
203             As this data is free and quite stable, the information about the INSEE
204             code, name are quite accurate.
205              
206             The information about the postal/zip code is however much more difficult to
207             get AND subject to change.
208              
209             I'll try to keep the postal/zip codes up to date, but anyone knowing a way to get
210             automatically these data could greatly contribute by telling it (or providing the data)
211              
212             Don't hesitate to contact me on this subject.
213              
214              
215             Arnaud (Arhuman) ASSAD, C<< >>
216              
217             =head1 AUTHOR
218              
219             Arnaud (Arhuman) ASSAD, C<< >>
220              
221             =head1 BUGS
222              
223             Please report any bugs or feature requests to C, or through
224             the web interface at L. I will be notified, and then you'll
225              
226             =head1 SUPPORT
227              
228             You can find documentation for this module with the perldoc command.
229              
230             perldoc Data::FR::Town
231              
232              
233             You can also look for information at:
234              
235             =over 4
236              
237             =item * RT: CPAN's request tracker (report bugs here)
238              
239             L
240              
241             =item * AnnoCPAN: Annotated CPAN documentation
242              
243             L
244              
245             =item * CPAN Ratings
246              
247             L
248              
249             =item * Search CPAN
250              
251             L
252              
253             =back
254              
255              
256             =head1 ACKNOWLEDGEMENTS
257              
258              
259             =head1 LICENSE AND COPYRIGHT
260              
261             Copyright 2012 Arnaud (Arhuman) ASSAD.
262              
263             This program is free software; you can redistribute it and/or modify it
264             under the terms of either: the GNU General Public License as published
265             by the Free Software Foundation; or the Artistic License.
266              
267             See http://dev.perl.org/licenses/ for more information.
268              
269              
270             =cut
271              
272             1; # End of Data::FR::Town
273              
274             __DATA__