File Coverage

blib/lib/Eircode.pm
Criterion Covered Total %
statement 50 52 96.1
branch 16 18 88.8
condition 4 5 80.0
subroutine 9 9 100.0
pod 3 4 75.0
total 82 88 93.1


line stmt bran cond sub pod time code
1 3     3   186979 use utf8;
  3         64  
  3         17  
2             # ABSTRACT: Validation and utilities for Eircodes / Irish postcodes
3             package Eircode;
4              
5             # Check Eircodes / Irish postcodes
6              
7              
8 3     3   131 use strict;
  3         8  
  3         59  
9 3     3   14 use Carp;
  3         6  
  3         191  
10 3     3   1341 use Const::Fast;
  3         7393  
  3         17  
11 3     3   1556 use parent qw< Exporter >;
  3         846  
  3         18  
12              
13             our @EXPORT_OK = ( qw<
14             check_eircode
15             normalise_eircode
16             split_eircode
17             > );
18             our $VERSION = "0.2.0";
19              
20              
21              
22             sub check_eircode{
23 25     25 1 7321 my( $data, $opt, @x ) = @_;
24 25 50       59 if( scalar @x ) {
25 0         0 croak 'Usage check_eircode($data, {});';
26             }
27              
28 25   100     84 $opt ||= {};
29              
30 25         43 my $strict = $opt->{strict};
31 25         31 my $lax = $opt->{lax};
32 25         38 my $space_optional = $opt->{space_optional};
33 25         44 my @options = grep{$_} ($strict, $lax, $space_optional);
  75         114  
34              
35 25 50       47 if( scalar @options > 1){
36 0         0 croak q{Can't combine options for strict/lax/space_optional at the moment};
37             }
38              
39 25 100       44 if( $lax ){
40 1         30 $data =~ tr/[ ]//d;
41             }
42              
43 25 100       71 unless($strict){
44 22         44 $data = uc($data);
45             }
46              
47 25 100       46 $data or return;
48              
49 24         45 my $re = build_re($opt);
50 24 100       49 if( $strict ){
51 3         32 return $data =~ /$re/;
52             }
53             else{
54 21         180 return $data =~ /$re/i;
55             }
56              
57             }
58              
59             const my $EIR_LETTER => 'A-NP-Z';
60             const my $LETTER_CLASS => "[$EIR_LETTER]";
61             const my $EIR_ANY => "[$EIR_LETTER\\d]";
62             const my $ROUTING_KEY => "${LETTER_CLASS}${EIR_ANY}{2}";
63             const my $UID => "${EIR_ANY}{4}";
64              
65             sub build_re{
66 24     24 0 37 my($opt) = @_;
67 24         33 my $lax = $opt->{lax};
68 24         37 my $space_optional = $opt->{space_optional};
69              
70 24         29 my $re;
71 24 100       49 if( $lax ){
    100          
72 1         42 $re = qr{^$ROUTING_KEY$UID$};
73             }
74             elsif( $space_optional ){
75 6         73 $re = qr{^$ROUTING_KEY\s*$UID$};
76             }
77             else{
78 17         139 $re = qr{^$ROUTING_KEY\s+$UID$};
79             }
80              
81             }
82              
83             sub normalise_eircode{
84 4     4 1 1129 my($input) = @_;
85 4         8 $input = uc $input;
86 4         12 $input =~ tr/ \t//d;
87 4         9 my($routing_key, $uid) = split_eircode($input);
88 3         12 return "$routing_key $uid";
89             }
90              
91              
92             sub split_eircode{
93 14     14 1 2887 my($input) = @_;
94 14         201 my( $routing_key, $uid ) = ($input =~ /^($ROUTING_KEY)\s*($UID)$/i);
95 14 100 66     95 $routing_key && $uid or die 'invalid eircode';
96 8         29 return ($routing_key, $uid);
97             }
98              
99              
100             ;1
101              
102             __END__