blib/lib/Eircode.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 # ABSTRACT: Validation and utilities for Eircodes / Irish postcodes
3 package Eircode;
4  
5 # Check Eircodes / Irish postcodes
6  
7  
8 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=2.6
11 use parent qw< Exporter >;
12  
13 our @EXPORT_OK = ( qw<
14 check_eircode
15 normalise_eircode
16 split_eircode
17 > );
18 our $VERSION = "0.1.0";
19  
20  
21  
22 sub check_eircode{
23 my( $data, $opt, @x ) = @_;
24 if( scalar @x ) {
25 croak 'Usage check_eircode($data, {});';
26 }
27  
28 $opt ||= {};
29  
30 my $strict = $opt->{strict};
31 my $lax = $opt->{lax};
32  
33 if( $strict && $lax ){
34 croak 'Cant be strict and lax at the same time';
35 }
36  
37 if( $lax ){
38 $data =~ tr/[ ]//d;
39 }
40  
41 unless($strict){
42 $data = uc($data);
43 }
44  
45 $data or return;
46  
47 my $re = build_re($opt);
48 if( $strict ){
49 return $data =~ /$re/;
50 }
51 else{
52 return $data =~ /$re/i;
53 }
54  
55 }
56  
57 const my $EIR_LETTER => 'A-NP-Z';
58 const my $LETTER_CLASS => "[$EIR_LETTER]";
59 const my $EIR_ANY => "[$EIR_LETTER\\d]";
60 const my $ROUTING_KEY => "${LETTER_CLASS}${EIR_ANY}{2}";
61 const my $UID => "${EIR_ANY}{4}";
62  
63 sub build_re{
64 my($opt) = @_;
65 my $lax = $opt->{lax};
66  
67 my $re;
68 if( $lax ){
69 $re = qr{^$ROUTING_KEY$UID$};
70 }
71 else{
72 $re = qr{^$ROUTING_KEY\s+$UID$};
73 }
74  
75 }
76  
77 sub normalise_eircode{
78 my($input) = @_;
79 $input = uc $input;
80 $input =~ tr/ \t//d;
81 my($routing_key, $uid) = split_eircode($input);
82 return "$routing_key $uid";
83 }
84  
85  
86 sub split_eircode{
87 my($input) = @_;
88 my( $routing_key, $uid ) = ($input =~ /^($ROUTING_KEY)\s*($UID)$/i);
89 $routing_key && $uid or die 'invalid eircode';
90 return ($routing_key, $uid);
91 }
92  
93  
94 ;1
95  
96 __END__
↑ Summary