| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Number::ZipCode::JP; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 4 |  |  | 4 |  | 334822 | use strict; | 
|  | 4 |  |  |  |  | 21 |  | 
|  | 4 |  |  |  |  | 221 |  | 
| 4 | 4 |  |  | 4 |  | 24 | use warnings; | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 4 |  |  |  |  | 128 |  | 
| 5 | 4 |  |  | 4 |  | 105 | use 5.008_001; | 
|  | 4 |  |  |  |  | 17 |  | 
| 6 | 4 |  |  | 4 |  | 26 | use Carp; | 
|  | 4 |  |  |  |  | 9 |  | 
|  | 4 |  |  |  |  | 416 |  | 
| 7 | 4 |  |  | 4 |  | 2283 | use UNIVERSAL::require; | 
|  | 4 |  |  |  |  | 4805 |  | 
|  | 4 |  |  |  |  | 43 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | our $VERSION = '0.20230831'; | 
| 10 |  |  |  |  |  |  | our %ZIP_TABLE = (); | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | sub import { | 
| 13 | 5 |  |  | 5 |  | 855 | my $self = shift; | 
| 14 | 5 |  |  |  |  | 71 | %ZIP_TABLE = (); | 
| 15 | 5 | 100 |  |  |  | 18 | if (@_) { | 
| 16 | 2 |  |  |  |  | 5 | my @packages = (); | 
| 17 | 2 |  |  |  |  | 5 | for my $subclass (@_) { | 
| 18 | 2 |  |  |  |  | 27 | push @packages, | 
| 19 |  |  |  |  |  |  | sprintf('%s::Table::%s', __PACKAGE__, ucfirst(lc($subclass))); | 
| 20 |  |  |  |  |  |  | } | 
| 21 | 2 |  |  |  |  | 12 | %ZIP_TABLE = _merge_table(@packages); | 
| 22 |  |  |  |  |  |  | } | 
| 23 |  |  |  |  |  |  | else { | 
| 24 | 3 |  |  |  |  | 929 | require Number::ZipCode::JP::Table; | 
| 25 | 3 |  |  |  |  | 17 | import  Number::ZipCode::JP::Table; | 
| 26 |  |  |  |  |  |  | } | 
| 27 | 5 |  |  |  |  | 1747 | return $self; | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | sub _merge_table { | 
| 31 | 4 |  |  | 4 |  | 13 | my %table = (); | 
| 32 | 4 |  |  |  |  | 10 | for my $pkg (@_) { | 
| 33 | 6 | 50 |  |  |  | 43 | $pkg->require or croak $@; | 
| 34 |  |  |  |  |  |  | { | 
| 35 | 4 |  |  | 4 |  | 881 | no strict 'refs'; | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 4 |  |  |  |  | 2418 |  | 
|  | 6 |  |  |  |  | 117 |  | 
| 36 | 6 |  |  |  |  | 12 | while (my($k, $v) = each %{"$pkg\::ZIP_TABLE"}) { | 
|  | 5691 |  |  |  |  | 17879 |  | 
| 37 | 5685 |  | 100 |  |  | 17343 | $table{$k} ||= []; | 
| 38 | 5685 |  |  |  |  | 7319 | push @{$table{$k}}, $v; | 
|  | 5685 |  |  |  |  | 11406 |  | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  | } | 
| 41 |  |  |  |  |  |  | } | 
| 42 | 4 |  |  |  |  | 1636 | return %table; | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | sub new { | 
| 46 | 3 |  |  | 3 | 1 | 1309 | my $class = shift; | 
| 47 | 3 |  |  |  |  | 8 | my $self = bless {}, $class; | 
| 48 | 3 | 50 |  |  |  | 12 | $self->set_number(@_) if @_; | 
| 49 | 3 |  |  |  |  | 10 | return $self; | 
| 50 |  |  |  |  |  |  | } | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | sub set_number { | 
| 53 | 148966 |  |  | 148966 | 1 | 334809 | my $self   = shift; | 
| 54 | 148966 |  |  |  |  | 290321 | my $number = shift; | 
| 55 | 148966 | 50 |  |  |  | 1120480 | if (ref($number) eq 'ARRAY') { | 
|  |  | 50 |  |  |  |  |  | 
|  |  | 50 |  |  |  |  |  | 
| 56 | 0 |  |  |  |  | 0 | $self->_prefix = shift @$number; | 
| 57 | 0 |  |  |  |  | 0 | $self->_suffix = shift @$number; | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  | elsif (defined $_[0]) { | 
| 60 | 0 |  |  |  |  | 0 | $self->_prefix = $number; | 
| 61 | 0 |  |  |  |  | 0 | $self->_suffix = $_[0]; | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  | elsif ($number =~ /^(\d{3})(?:\D)?(\d{4})$/) { | 
| 64 | 148966 |  |  |  |  | 409683 | my $pref = $1; | 
| 65 | 148966 |  |  |  |  | 277298 | my $suff = $2; | 
| 66 | 148966 |  |  |  |  | 394201 | $self->_prefix = $pref; | 
| 67 | 148966 |  |  |  |  | 382886 | $self->_suffix = $suff; | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  | else { | 
| 70 | 0 |  |  |  |  | 0 | carp "The number is invalid zip-code."; | 
| 71 | 0 |  |  |  |  | 0 | $self->_prefix = (); | 
| 72 | 0 |  |  |  |  | 0 | $self->_suffix = (); | 
| 73 |  |  |  |  |  |  | } | 
| 74 | 148966 |  |  |  |  | 1079432 | return $self; | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | sub is_valid_number { | 
| 78 | 148966 |  |  | 148966 | 1 | 238798 | my $self = shift; | 
| 79 | 148966 |  |  |  |  | 275532 | my $pref = $self->_prefix; | 
| 80 | 148966 |  |  |  |  | 301291 | my $suff = $self->_suffix; | 
| 81 | 148966 | 50 | 33 |  |  | 389672 | unless ($pref || $suff) { | 
| 82 | 0 |  |  |  |  | 0 | carp "Any number was not set"; | 
| 83 | 0 |  |  |  |  | 0 | return; | 
| 84 |  |  |  |  |  |  | } | 
| 85 | 148966 | 50 | 33 |  |  | 838274 | return unless $pref =~ /^\d{3}$/ && $suff =~ /^\d{4}$/; | 
| 86 | 148966 |  |  |  |  | 369435 | my $re_ref = $ZIP_TABLE{$pref}; | 
| 87 | 148966 | 50 | 33 |  |  | 577342 | return unless defined $re_ref && ref($re_ref) eq 'ARRAY'; | 
| 88 | 148966 |  |  |  |  | 246639 | my $matched; | 
| 89 | 148966 |  |  |  |  | 327058 | for my $re (@$re_ref) { | 
| 90 | 148966 | 100 |  |  |  | 1346530 | if ($suff =~ /^$re$/) { | 
| 91 | 147073 |  |  |  |  | 283198 | $matched = 1; | 
| 92 | 147073 |  |  |  |  | 346908 | last; | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  | } | 
| 95 | 148966 |  |  |  |  | 1227735 | return $matched; | 
| 96 |  |  |  |  |  |  | } | 
| 97 |  |  |  |  |  |  |  | 
| 98 | 297932 |  |  | 297932 |  | 643632 | sub _prefix : lvalue { shift->{_prefix} } | 
| 99 | 297932 |  |  | 297932 |  | 534592 | sub _suffix : lvalue { shift->{_suffix} } | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | 1; | 
| 102 |  |  |  |  |  |  | __END__ |