File Coverage

blib/lib/Geo/LookupPostcode/FI.pm
Criterion Covered Total %
statement 25 26 96.1
branch 4 6 66.6
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 37 40 92.5


line stmt bran cond sub pod time code
1             package Geo::LookupPostcode::FI;
2              
3 1     1   13042 use 5.012;
  1         2  
4 1     1   3 use strict;
  1         1  
  1         25  
5 1     1   3 use warnings FATAL => 'all';
  1         2  
  1         28  
6 1     1   3 use utf8;
  1         1  
  1         4  
7              
8 1     1   16 use Carp qw(croak);
  1         1  
  1         338  
9             require Exporter;
10              
11             our @ISA = qw(Exporter);
12             our @EXPORT_OK = qw(lookup_fi_postcode);
13              
14             my %geo_areas;
15              
16             =encoding utf-8
17              
18             =head1 NAME
19              
20             Geo::LookupPostcode::FI - Get province and region codes for a Finnish postcode
21              
22             =head1 SUBROUTINES/METHODS
23              
24             =head2 lookup_fi_postcode
25              
26             Takes one character string argument: a postcode.
27              
28             If successful, it returns a reference to a hash, with this structure:
29              
30              
31             my $rh_area = lookup_fi_postcode("01430");
32             {
33             area_name => $area_name,
34             }
35            
36             If it cannot find the area, it returns undef.
37              
38             Note that the names may be anglicised. Note that the areas given here are
39             approximate, as real postal code districts seldom follow administrative region
40             or province boundaries, and sometimes even a single municipality can be located
41             in multiple postal code districts.
42              
43             =cut
44              
45             sub lookup_fi_postcode {
46 100001 50   100001 1 21795327 croak "Expected one argument" if (@_ != 1);
47 100001         94905 my ($postcode) = @_;
48              
49             # Do some cleanup of postcode:
50 100001         124758 utf8::upgrade $postcode;
51 100001         550826 $postcode =~ s/\s*//g;
52              
53 100001 100       229730 return if $postcode !~ /^[0-9]+$/;
54              
55 100000         218484 $postcode = sprintf("%05d", $postcode);
56              
57 100000         161824 my $area_code = substr($postcode, 0, 2);
58              
59 100000         101022 my $area_name = $geo_areas{$area_code};
60 100000 50       142597 if (defined($area_name)) {
61 100000         228946 return { area_name => $area_name };
62             }
63 0           return;
64             }
65              
66             # Data from https://en.wikipedia.org/wiki/List_of_postal_codes_in_Finland
67              
68 1     1   84 BEGIN { %geo_areas = (
69             '00' => 'Helsinki',
70             '01' => 'Vantaa',
71             '02' => 'the Espoo',
72             '03' => 'north-western Uusimaa',
73             '04' => 'north-eastern Uusimaa',
74             '05' => 'Hyvinkää',
75             '06' => 'Porvoo',
76             '07' => 'the rest of Eastern Uusimaa',
77             '08' => 'Lohja',
78             '09' => 'the rest of the Lohja region',
79             '10' => 'South Western Uusimaa, Hanko',
80             '11' => 'Riihimäki',
81             '12' => 'the rest of the Riihimäki region',
82             '13' => 'Hämeenlinna',
83             '14' => 'the rest of Tavastia Proper',
84             '15' => 'the Lahti region',
85             '16' => 'southern Päijänne Tavastia',
86             '17' => 'the west bank of Lake Päijänne',
87             '18' => 'Heinola',
88             '19' => 'north-eastern Päijänne Tavastia',
89             '20' => 'Turku',
90             '21' => 'the rest of the Turku region',
91             '22' => 'Åland',
92             '23' => 'the Uusikaupunki region (Vakka-Suomi)',
93             '24' => 'Salo, Finland',
94             '25' => 'the rest of western Southwest Finland',
95             '26' => 'Rauma, Finland',
96             '27' => 'the rest of southern Satakunta',
97             '28' => 'Pori',
98             '29' => 'the rest of northern Satakunta',
99             '30' => 'Forssa',
100             '31' => 'the rest of the Forssa region',
101             '32' => 'the Loimaa region',
102             '33' => 'the Tampere region',
103             '34' => 'northern Pirkanmaa',
104             '35' => 'north-eastern Pirkanmaa',
105             '36' => 'south-eastern Pirkanmaa',
106             '37' => 'south-western Pirkanmaa',
107             '38' => 'eastern Satakunta',
108             '39' => 'north-western Pirkanmaa',
109             '40' => 'the Jyväskylä region',
110             '41' => 'the eastern part of Central Finland',
111             '42' => 'the Jämsä and Keuruu regions',
112             '43' => 'northern Central Finland',
113             '44' => 'the Äänekoski region',
114             '45' => 'Kouvola',
115             '46' => 'Anjalankoski',
116             '47' => 'north-western Kymenlaakso',
117             '48' => 'Kotka',
118             '49' => 'the Hamina region',
119             '50' => 'Mikkeli',
120             '51' => 'the northern part of Southern Savonia',
121             '52' => 'the south-eastern part of Southern Savonia',
122             '53' => 'Lappeenranta',
123             '54' => 'the rest of the Lappeenranta region',
124             '55' => 'Imatra',
125             '56' => 'Rautjärvi and Ruokolahti',
126             '57' => 'Savonlinna',
127             '58' => 'the western part of Southern Savonia',
128             '59' => 'the rest of South Karelia',
129             '60' => 'the Seinäjoki region',
130             '61' => 'the western part of Southern Ostrobothnia',
131             '62' => 'the northern part of Southern Ostrobothnia',
132             '63' => 'the eastern part of Southern Ostrobothnia',
133             '64' => 'southern Ostrobothnia (region)',
134             '65' => 'Vaasa',
135             '66' => 'northern Ostrobothnia',
136             '67' => 'Kokkola',
137             '68' => 'the Jakobstad',
138             '69' => 'the rest of Central Ostrobothnia',
139             '70' => 'Kuopio',
140             '71' => 'the rest of the Kuopio region',
141             '72' => 'the western part of Northern Savonia',
142             '73' => 'the eastern part of Northern Savonia',
143             '74' => 'the Iisalmi region',
144             '75' => 'Nurmes',
145             '76' => 'Pieksämäki',
146             '77' => 'Suonenjoki',
147             '78' => 'Varkaus',
148             '79' => 'the rest of the Varkaus region',
149             '80' => 'Joensuu',
150             '81' => 'northern North Karelia',
151             '82' => 'southern North Karelia',
152             '83' => 'western North Karelia',
153             '84' => 'Ylivieska',
154             '85' => 'the Nivala region',
155             '86' => 'the central part of Northern Ostrobothnia',
156             '87' => 'Kajaani',
157             '88' => 'southern Kainuu',
158             '89' => 'northern Kainuu',
159             '90' => 'the Oulu region',
160             '91' => 'the northern part of Northern Ostrobothnia',
161             '92' => 'the Raahe region',
162             '93' => 'the eastern part of Northern Ostrobothnia',
163             '94' => 'Kemi',
164             '95' => 'the Tornio region and western',
165             '96' => 'Rovaniemi',
166             '97' => 'Ranua and Posio',
167             '98' => 'central Lapland',
168             '99' => 'northern Lapland',
169             # '99999' => 'Korvatunturi', # fictional
170             ); }
171              
172              
173             1;