File Coverage

blib/lib/Eircode.pm
Criterion Covered Total %
statement 51 52 98.0
branch 17 18 94.4
condition 4 5 80.0
subroutine 9 9 100.0
pod 3 4 75.0
total 84 88 95.4


line stmt bran cond sub pod time code
1 3     3   148112 use utf8;
  3         50  
  3         11  
2             # ABSTRACT: Validation and utilities for Eircodes / Irish postcodes
3             package Eircode;
4              
5             # Check Eircodes / Irish postcodes
6              
7              
8 3     3   95 use strict;
  3         5  
  3         43  
9 3     3   10 use Carp;
  3         4  
  3         135  
10 3     3   1008 use Const::Fast;
  3         5710  
  3         14  
11 3     3   1178 use parent qw< Exporter >;
  3         681  
  3         14  
12              
13             our @EXPORT_OK = ( qw<
14             check_eircode
15             normalise_eircode
16             split_eircode
17             > );
18             our $VERSION = "0.2.1";
19              
20              
21              
22             sub check_eircode{
23 26     26 1 6112 my( $data, $opt, @x ) = @_;
24 26 100       61 if( scalar @x ) {
25 1         195 croak 'Usage check_eircode($data, {});';
26             }
27              
28 25   100     66 $opt ||= {};
29              
30 25         28 my $strict = $opt->{strict};
31 25         37 my $lax = $opt->{lax};
32 25         36 my $space_optional = $opt->{space_optional};
33 25         38 my @options = grep{$_} ($strict, $lax, $space_optional);
  75         108  
34              
35 25 50       42 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       31 if( $lax ){
40 1         15 $data =~ tr/[ ]//d;
41             }
42              
43 25 100       33 unless($strict){
44 22         32 $data = uc($data);
45             }
46              
47 25 100       34 $data or return;
48              
49 24         40 my $re = build_re($opt);
50 24 100       54 if( $strict ){
51 3         15 return $data =~ /$re/;
52             }
53             else{
54 21         143 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 31 my($opt) = @_;
67 24         26 my $lax = $opt->{lax};
68 24         20 my $space_optional = $opt->{space_optional};
69              
70 24         36 my $re;
71 24 100       37 if( $lax ){
    100          
72 1         32 $re = qr{^$ROUTING_KEY$UID$};
73             }
74             elsif( $space_optional ){
75 6         54 $re = qr{^$ROUTING_KEY\s*$UID$};
76             }
77             else{
78 17         100 $re = qr{^$ROUTING_KEY\s+$UID$};
79             }
80              
81             }
82              
83             sub normalise_eircode{
84 4     4 1 941 my($input) = @_;
85 4         8 $input = uc $input;
86 4         9 $input =~ tr/ \t//d;
87 4         6 my($routing_key, $uid) = split_eircode($input);
88 3         7 return "$routing_key $uid";
89             }
90              
91              
92             sub split_eircode{
93 14     14 1 2427 my($input) = @_;
94 14         165 my( $routing_key, $uid ) = ($input =~ /^($ROUTING_KEY)\s*($UID)$/i);
95 14 100 66     81 $routing_key && $uid or die 'invalid eircode';
96 8         21 return ($routing_key, $uid);
97             }
98              
99              
100             ;1
101              
102             __END__