| 1 | package Number::Phone::UK; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 17 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 17 | ▶ | use Scalar::Util 'blessed'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 17 | ▶ | use Number::Phone::UK::Data; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 17 | ▶ | use base 'Number::Phone'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 16 | our $VERSION = '1.72'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 16 | my $cache = {}; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 NAME | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | Number::Phone::UK - UK-specific methods for Number::Phone | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | use Number::Phone; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | $daves_phone = Number::Phone->new('+44 1234 567890'); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =cut | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 347 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 347 | my $class = shift; | |||||||||||||||||||||||||||
| 28 | 347 | my $number = shift; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 347 | $number = '+44'._clean_number($number); | |||||||||||||||||||||||||||
| 31 | 347 | ▶ | if(is_valid($number)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 312 | $number =~ s/^0/+44/; | |||||||||||||||||||||||||||
| 33 | 312 | my $target_class = $class->_get_class(_clean_number($number)); | |||||||||||||||||||||||||||
| 34 | 312 | ▶ | return undef if($class ne $target_class); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 306 | return bless(\$number, $target_class); | |||||||||||||||||||||||||||
| 36 | 36 | } else { return undef; } | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =head1 DATABASE | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | Number::Phone::UK uses a large database, access via L<Number::Phone::UK::Data>. This | ||||||||||||||||||||||||||||
| 42 | database lives in a file, and normally only the little bits of it that you access will | ||||||||||||||||||||||||||||
| 43 | ever get loaded into memory. This means, however, that creating Number::Phone::UK objects | ||||||||||||||||||||||||||||
| 44 | almost always involves disk access and so is slow compared to data for some other | ||||||||||||||||||||||||||||
| 45 | countries. There are two ways to avoid this slowness. | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | First, if you don't need all the functionality you can use L<Number::Phone::Lib>. | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | Second, if you can accept slow startup - eg when your server starts - then you can call | ||||||||||||||||||||||||||||
| 50 | C<< Number::Phone::UK::Data->slurp() >> from your code, which will pull the entire database | ||||||||||||||||||||||||||||
| 51 | into memory. This will take a few minutes, and on a 64-bit machine will consume of the | ||||||||||||||||||||||||||||
| 52 | order of 200MB of memory. | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | The database uses L<Data::CompactReadonly>. This may have some problems if you connect to it, | ||||||||||||||||||||||||||||
| 55 | C<fork()>, and then try to access the database from multiple processes. We attempt to | ||||||||||||||||||||||||||||
| 56 | work around this by re-connecting to the database after forking. This is, of course, | ||||||||||||||||||||||||||||
| 57 | not a problem if you C<slurp()> the database before forking. | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =head1 METHODS | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | The following methods from Number::Phone are overridden: | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =over 4 | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =item new | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | The constructor, you should never have to call this yourself. To create an | ||||||||||||||||||||||||||||
| 68 | object the canonical incantation is C<< Number::Phone->new('+44 ...') >>. | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =item data_source | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | Returns a string telling where and when the data that drives this class was last updated, looking something like: | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | "OFCOM at Wed Sep 30 10:37:39 2020 UTC" | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | The current value of this is also documented in L<Number::Phone::Data>. | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =item is_valid | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | The number is valid within the national numbering scheme. It may or may | ||||||||||||||||||||||||||||
| 81 | not yet be allocated, or it may be reserved. Any number which returns | ||||||||||||||||||||||||||||
| 82 | true for any of the following methods will also be valid. | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =cut | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 312 | ▶ | sub _get_class { | ||||||||||||||||||||||||||
|
Subroutine
_get_class: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 87 | 312 | my $class = shift; | |||||||||||||||||||||||||||
| 88 | 312 | my $number = shift; | |||||||||||||||||||||||||||
| 89 | 312 | foreach my $prefix (_prefixes($number)) { | |||||||||||||||||||||||||||
| 90 | 2935 | ▶ | if(exists(Number::Phone::UK::Data::db()->{subclass}->{$prefix})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 47 | ▶ | return $class if(Number::Phone::UK::Data::db()->{subclass}->{$prefix} eq ''); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 47 | my $desired_subclass = Number::Phone::UK::Data::db()->{subclass}->{$prefix}; | |||||||||||||||||||||||||||
| 94 | 47 | my $subclass = "Number::Phone::UK::$desired_subclass"; | |||||||||||||||||||||||||||
| 95 | 47 | ▶ | eval "use $subclass"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 96 | 47 | return $subclass; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 265 | return $class; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 1225 | ▶ | sub _clean_number { | ||||||||||||||||||||||||||
|
Subroutine
_clean_number: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 103 | 1224 | my $clean = shift; | |||||||||||||||||||||||||||
| 104 | 1224 | $clean =~ s/[^0-9+]//g; # strip non-digits/plusses | |||||||||||||||||||||||||||
| 105 | 1224 | $clean =~ s/^\+44//; # remove leading +44 | |||||||||||||||||||||||||||
| 106 | 1224 | $clean =~ s/^0//; # kill leading zero | |||||||||||||||||||||||||||
| 107 | 1224 | return $clean; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 799 | ▶ | sub _prefixes { | ||||||||||||||||||||||||||
|
Subroutine
_prefixes: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 111 | 798 | my $number = shift; | |||||||||||||||||||||||||||
| 112 | 798 | map { substr($number, 0, $_) } reverse(1..length($number)); | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 376 | ▶ | sub is_valid { | ||||||||||||||||||||||||||
|
Subroutine
is_valid: called CC=17 SCAR=28.3
Pod
documented
| |||||||||||||||||||||||||||||
| 116 | 375 | my $number = shift; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | # If called as an object method, it *must* be valid otherwise the | ||||||||||||||||||||||||||||
| 119 | # object would never have been instantiated. | ||||||||||||||||||||||||||||
| 120 | # If called as a subroutine, that's the constructor doing its thang. | ||||||||||||||||||||||||||||
| 121 | 375 | ▶ | return 1 if(blessed($number)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # otherwise we have to validate | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | # if we've seen this number before, use cached result | ||||||||||||||||||||||||||||
| 126 | 346 | ▶ | return 1 if($cache->{$number}->{is_valid}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # assume it's OK unless proven otherwise | ||||||||||||||||||||||||||||
| 129 | 199 | $cache->{$number}->{is_valid} = 1; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 199 | my $cleaned_number = _clean_number($number); | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 199 | my @prefixes = _prefixes($cleaned_number); | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | # quickly check length | ||||||||||||||||||||||||||||
| 136 | 199 | ▶ | return $cache->{$number}->{is_valid} = 0 if(length($cleaned_number) < 7 || length($cleaned_number) > 10); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
length $cleaned_number < 7 or length $cleaned_number > 10
MC/DC100%2/2
length $cleaned_number < 7 or length $cleaned_number > 10
length $cleaned_number < 7
length $cleaned_number > 10
Truth table100%3/3
A or B
A= length $cleaned_number < 7B= length $cleaned_number > 10
| |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | # 04 and 06 are invalid, only 05[56] are valid | ||||||||||||||||||||||||||||
| 139 | 182 | ▶ | return $cache->{$number}->{is_valid} = 0 if($cleaned_number =~ /^(4|5[01234789]|6)/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # slightly more rigourous length check for some unallocated geographic numbers | ||||||||||||||||||||||||||||
| 142 | # 07, 02x and 011x are always ten digits | ||||||||||||||||||||||||||||
| 143 | 176 | ▶ | return $cache->{$number}->{is_valid} = 0 if($cleaned_number =~ /^([27]|11)/ && length($cleaned_number) != 10); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$cleaned_number =~ /^([27]|11)/ and length $cleaned_number != 10
MC/DC100%2/2
$cleaned_number =~ /^([27]|11)/ and length $cleaned_number != 10
$cleaned_number =~ /^([27]|11)/
length $cleaned_number != 10
Truth table100%3/3
A and B
A= $cleaned_number =~ /^([27]|11)/B= length $cleaned_number != 10
| |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 168 | my $telco; | |||||||||||||||||||||||||||
| 146 | my $format; | ||||||||||||||||||||||||||||
| 147 | 168 | foreach my $prefix (@prefixes) { | |||||||||||||||||||||||||||
| 148 | 875 | ▶ | if(exists(Number::Phone::UK::Data::db()->{telco}->{$prefix})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 162 | $telco = Number::Phone::UK::Data::db()->{telco}->{$prefix}; | |||||||||||||||||||||||||||
| 150 | 162 | last; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | 168 | foreach my $prefix (@prefixes) { | |||||||||||||||||||||||||||
| 154 | 875 | ▶ | if(exists(Number::Phone::UK::Data::db()->{format}->{$prefix})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 162 | $format = Number::Phone::UK::Data::db()->{format}->{$prefix}; | |||||||||||||||||||||||||||
| 156 | 162 | last; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 168 | $cache->{$number}->{is_allocated} = 0; | |||||||||||||||||||||||||||
| 161 | 168 | $cache->{$number}->{format} = $format; | |||||||||||||||||||||||||||
| 162 | 168 | ▶ | if($telco) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 107 | $cache->{$number}->{is_allocated} = 1; | |||||||||||||||||||||||||||
| 164 | 107 | $cache->{$number}->{operator} = $telco; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 168 | ▶ | if($cache->{$number}->{format} && $cache->{$number}->{format} =~ /\+/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$cache->{$number}{"format"} and $cache->{$number}{"format"} =~ /\+/
MC/DC50%1/2
$cache->{$number}{"format"} and $cache->{$number}{"format"} =~ /\+/
$cache->{$number}{"format"}
$cache->{$number}{"format"} =~ /\+/
Truth table66%2/3
A and B
A= $cache->{$number}{"format"}B= $cache->{$number}{"format"} =~ /\+/
| |||||||||||||||||||||||||||||
| 168 | 162 | my($arealength, $subscriberlength) = split(/\+/, $cache->{$number}->{format}); | |||||||||||||||||||||||||||
| 169 | # for hateful mixed thing | ||||||||||||||||||||||||||||
| 170 | 162 | ▶ | my @subscriberlengths = ($subscriberlength =~ m{/}) ? split(/\//, $subscriberlength) : ($subscriberlength); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 162 | $subscriberlength =~ s/^(\d+).*/$1/; # for hateful mixed thing | |||||||||||||||||||||||||||
| 172 | 162 | $cache->{$number}->{areacode} = substr($cleaned_number, 0, $arealength); | |||||||||||||||||||||||||||
| 173 | 162 | $cache->{$number}->{subscriber} = substr($cleaned_number, $arealength); | |||||||||||||||||||||||||||
| 174 | $cache->{$number}->{areaname} = ( | ||||||||||||||||||||||||||||
| 175 | map { | ||||||||||||||||||||||||||||
| 176 | 91 | Number::Phone::UK::Data::db()->{areanames}->{$_} | |||||||||||||||||||||||||||
| 177 | } grep { | ||||||||||||||||||||||||||||
| 178 | 162 | exists(Number::Phone::UK::Data::db()->{areanames}->{$_}) | |||||||||||||||||||||||||||
| 179 | } @prefixes | ||||||||||||||||||||||||||||
| 180 | )[0]; | ||||||||||||||||||||||||||||
| 181 | 162 | ▶ | if(!grep { length($cache->{$number}->{subscriber}) == $_ } @subscriberlengths) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | # number wrong length! | ||||||||||||||||||||||||||||
| 183 | 4 | $cache->{$number} = { is_valid => 0 }; | |||||||||||||||||||||||||||
| 184 | 4 | return 0; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 164 | return $cache->{$number}->{is_valid}; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | # now define the is_* methods that we over-ride | ||||||||||||||||||||||||||||
| 192 | 31 | ▶ | sub is_fixed_line { | ||||||||||||||||||||||||||
|
Subroutine
is_fixed_line: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 193 | 30 | ▶ | return 0 if(is_mobile(@_)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | 16 | return undef; | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 80 | ▶ | sub is_drama { | ||||||||||||||||||||||||||
|
Subroutine
is_drama: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 198 | 80 | my $self = shift; | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 80 | my $num = _clean_number(${$self}); | |||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 80 | my @drama_numbers = ( | |||||||||||||||||||||||||||
| 203 | # Leeds, Sheffield, Nottingham, Leicester, Bristol, Reading | ||||||||||||||||||||||||||||
| 204 | qr/^11[3-8]4960[0-9]{3}$/, | ||||||||||||||||||||||||||||
| 205 | # Birmingham, Edinburgh, Glasgow, Liverpool, Manchester | ||||||||||||||||||||||||||||
| 206 | qr/^1[2-6]14960[0-9]{3}$/, | ||||||||||||||||||||||||||||
| 207 | # London | ||||||||||||||||||||||||||||
| 208 | qr/^2079460[0-9]{3}$/, | ||||||||||||||||||||||||||||
| 209 | # Tyneside/Durham/Sunderland | ||||||||||||||||||||||||||||
| 210 | qr/^1914980[0-9]{3}$/, | ||||||||||||||||||||||||||||
| 211 | # Northern Ireland | ||||||||||||||||||||||||||||
| 212 | qr/^2896496[0-9]{3}$/, | ||||||||||||||||||||||||||||
| 213 | # Cardiff | ||||||||||||||||||||||||||||
| 214 | qr/^2920180[0-9]{3}$/, | ||||||||||||||||||||||||||||
| 215 | # No area | ||||||||||||||||||||||||||||
| 216 | qr/^1632960[0-9]{3}$/, | ||||||||||||||||||||||||||||
| 217 | # Mobile | ||||||||||||||||||||||||||||
| 218 | qr/^7700900[0-9]{3}$/, | ||||||||||||||||||||||||||||
| 219 | # Freephone | ||||||||||||||||||||||||||||
| 220 | qr/^8081570[0-9]{3}$/, | ||||||||||||||||||||||||||||
| 221 | # Premium Rate | ||||||||||||||||||||||||||||
| 222 | qr/^9098790[0-9]{3}$/, | ||||||||||||||||||||||||||||
| 223 | # UK Wide | ||||||||||||||||||||||||||||
| 224 | qr/^3069990[0-9]{3}$/, | ||||||||||||||||||||||||||||
| 225 | ); | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 80 | foreach my $d (@drama_numbers) { | |||||||||||||||||||||||||||
| 228 | 552 | ▶ | return 1 if ($num =~ $d); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 32 | return 0; | |||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 16 | foreach my $is (qw( | |||||||||||||||||||||||||||
| 235 | geographic network_service tollfree corporate | ||||||||||||||||||||||||||||
| 236 | personal pager mobile specialrate adult allocated ipphone | ||||||||||||||||||||||||||||
| 237 | )) { | ||||||||||||||||||||||||||||
| 238 | 17 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 239 | 176 | ▶ | *{__PACKAGE__."::is_$is"} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 240 | 536 | my $self = shift; | |||||||||||||||||||||||||||
| 241 | 536 | ▶ | if(!exists($cache->{${$self}}->{"is_$is"})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 242 | 284 | $cache->{${$self}}->{"is_$is"} = | |||||||||||||||||||||||||||
| 243 | grep { | ||||||||||||||||||||||||||||
| 244 | exists( | ||||||||||||||||||||||||||||
| 245 | Number::Phone::UK::Data::db()->{ | ||||||||||||||||||||||||||||
| 246 | { geographic => 'geo_prefices', | ||||||||||||||||||||||||||||
| 247 | network_service => 'network_svc_prefices', | ||||||||||||||||||||||||||||
| 248 | tollfree => 'free_prefices', | ||||||||||||||||||||||||||||
| 249 | corporate => 'corporate_prefices', | ||||||||||||||||||||||||||||
| 250 | personal => 'personal_prefices', | ||||||||||||||||||||||||||||
| 251 | pager => 'pager_prefices', | ||||||||||||||||||||||||||||
| 252 | mobile => 'mobile_prefices', | ||||||||||||||||||||||||||||
| 253 | specialrate => 'special_prefices', | ||||||||||||||||||||||||||||
| 254 | adult => 'adult_prefices', | ||||||||||||||||||||||||||||
| 255 | ipphone => 'ip_prefices' | ||||||||||||||||||||||||||||
| 256 | }->{$is} | ||||||||||||||||||||||||||||
| 257 | 2838 | }->{$_} | |||||||||||||||||||||||||||
| 258 | ); | ||||||||||||||||||||||||||||
| 259 | 284 | } _prefixes(_clean_number(${$self})); | |||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||
| 261 | 536 | $cache->{${$self}}->{"is_$is"}; | |||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||
| 263 | 176 | } | |||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | # define the other methods | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 16 | foreach my $method (qw(operator areacode areaname subscriber)) { | |||||||||||||||||||||||||||
| 268 | 17 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 269 | 64 | ▶ | *{__PACKAGE__."::$method"} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 270 | 441 | my $self = shift; | |||||||||||||||||||||||||||
| 271 | 441 | return $cache->{${$self}}->{$method}; | |||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | 64 | } | |||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | =item is_allocated | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | The number has been allocated to a telco for use. It may or may not yet | ||||||||||||||||||||||||||||
| 278 | be in use or may be reserved. | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | =item is_drama | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | The number is intended for use in fiction. OFCOM has allocated numerous small | ||||||||||||||||||||||||||||
| 283 | ranges for this purpose. These numbers will not be allocated to real customers. | ||||||||||||||||||||||||||||
| 284 | See L<http://stakeholders.ofcom.org.uk/telecoms/numbering/guidance-tele-no/numbers-for-drama> | ||||||||||||||||||||||||||||
| 285 | for the authoritative source. | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | =item is_geographic | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | The number refers to a geographic area. | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | =item is_fixed_line | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | The number, when in use, can only refer to a fixed line. | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | (we can't tell whether a number is a fixed line, but we can tell that | ||||||||||||||||||||||||||||
| 296 | some are *not*). | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | =item is_mobile | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | The number, when in use, can only refer to a mobile phone. | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | =item is_pager | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | The number, when in use, can only refer to a pager. | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | =item is_tollfree | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | Callers will not be charged for calls to this number under normal circumstances. | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | =item is_specialrate | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | The number, when in use, attracts special rates. For instance, national | ||||||||||||||||||||||||||||
| 313 | dialling at local rates, or premium rates for services. | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | =item is_adult | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | The number, when in use, goes to a service of an adult nature, such as porn. | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | =item is_personal | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | The number, when in use, goes to an individual person. | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | =item is_corporate | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | The number, when in use, goes to a business. | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | =item is_ipphone | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | The number, when in use, is terminated using VoIP. | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | =item is_network_service | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | The number is some kind of network service such as a human operator, directory | ||||||||||||||||||||||||||||
| 334 | enquiries, emergency services etc | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | =item country_code | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | Returns 44. | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | =cut | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | 118 | ▶ | sub country_code { 44; } | ||||||||||||||||||||||||||
|
Subroutine
country_code: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | =item regulator | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | Returns informational text. | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | =cut | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | 2 | ▶ | sub regulator { 'OFCOM, http://www.ofcom.org.uk/'; } | ||||||||||||||||||||||||||
|
Subroutine
regulator: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | =item areacode | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | Return the area code - if applicable - for the number. If not applicable, | ||||||||||||||||||||||||||||
| 355 | returns undef. | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | =item areaname | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | Return the area name - if applicable - for the number, or undef. | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | =item location | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | For geographic numbers, this returns the location of the exchange to which | ||||||||||||||||||||||||||||
| 364 | that number is assigned, if available. Otherwise returns undef. | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | =cut | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | 6 | ▶ | sub location { | ||||||||||||||||||||||||||
|
Subroutine
location: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 369 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | 6 | ▶ | return undef unless($self->is_geographic()); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | 4 | my $cleaned_number = _clean_number(${$self}); | |||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | 4 | my @prefixes = _prefixes($cleaned_number); | |||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | # uncoverable branch true | ||||||||||||||||||||||||||||
| 378 | 4 | ▶ | if(!$ENV{TESTINGKILLTHEWABBIT}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 379 | 0 | eval "require Number::Phone::UK::DetailedLocations"; # uncoverable statement | |||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||
| 381 | 4 | ▶ | require Number::Phone::UK::Exchanges if(!$Number::Phone::UK::Exchanges::db); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | 4 | foreach(@prefixes) { | |||||||||||||||||||||||||||
| 384 | 28 | ▶ | if(exists($Number::Phone::UK::Exchanges::db->{exchg_prefices}->{$_})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 385 | return [ | ||||||||||||||||||||||||||||
| 386 | $Number::Phone::UK::Exchanges::db->{exchg_positions}->{$Number::Phone::UK::Exchanges::db->{exchg_prefices}->{$_}}->{lat}, | ||||||||||||||||||||||||||||
| 387 | $Number::Phone::UK::Exchanges::db->{exchg_positions}->{$Number::Phone::UK::Exchanges::db->{exchg_prefices}->{$_}}->{long} | ||||||||||||||||||||||||||||
| 388 | 4 | ]; | |||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||
| 391 | # may become coverable if I ever test the location of a number | ||||||||||||||||||||||||||||
| 392 | # in an areacode that wasn't in the data dump I got years ago | ||||||||||||||||||||||||||||
| 393 | 0 | return undef; # uncoverable statement | |||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | =item subscriber | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | Return the subscriber part of the number | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | =item operator | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | Return the name of the telco operating this number, in an appropriate | ||||||||||||||||||||||||||||
| 403 | character set and with optional details such as their web site or phone | ||||||||||||||||||||||||||||
| 404 | number. | ||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | =item format | ||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | Return a sanely formatted version of the number, complete with IDD code, eg | ||||||||||||||||||||||||||||
| 409 | for the UK number (0208) 771-2924 it would return +44 20 8771 2924. | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | =cut | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | 77 | ▶ | sub format { | ||||||||||||||||||||||||||
|
Subroutine
format: called CC=9 SCAR=22.6
Pod
documented
| |||||||||||||||||||||||||||||
| 414 | 77 | my $self = shift; | |||||||||||||||||||||||||||
| 415 | 77 | my $r; | |||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | 77 | ▶ | if($self->areacode()) { # if there's an areacode ... | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 418 | 50 | $r = '+'.country_code().' '.$self->areacode().' '; | |||||||||||||||||||||||||||
| 419 | 50 | ▶ | if( length($self->subscriber()) == 7) { $r .= substr($self->subscriber(), 0, 3).' '.substr($self->subscriber(), 3) } | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 420 | 23 | elsif(length($self->subscriber()) == 8) { $r .= substr($self->subscriber(), 0, 4).' '.substr($self->subscriber(), 4) } | |||||||||||||||||||||||||||
| 421 | 25 | else { $r .= $self->subscriber() } | |||||||||||||||||||||||||||
| 422 | ▶ | } elsif($self->subscriber && $self->subscriber =~ /^7/) { # mobiles/pagers don't have areacodes but should be formatted as if they do | |||||||||||||||||||||||||||
|
Condition66%2/3
$self->subscriber and $self->subscriber =~ /^7/
MC/DC50%1/2
$self->subscriber and $self->subscriber =~ /^7/
$self->subscriber
$self->subscriber =~ /^7/
Truth table66%2/3
A and B
A= $self->subscriberB= $self->subscriber =~ /^7/
| |||||||||||||||||||||||||||||
| 423 | 9 | $r = '+'.country_code(). | |||||||||||||||||||||||||||
| 424 | ' '.substr($self->subscriber(), 0, 4). | ||||||||||||||||||||||||||||
| 425 | ' '.substr($self->subscriber(), 4); | ||||||||||||||||||||||||||||
| 426 | ▶ | } elsif(!$self->is_allocated() || !$cache->{${self}}->{format}) { # if not allocated or no format | |||||||||||||||||||||||||||
|
Condition66%2/3
$self->is_allocated && $cache->{$self}{'format'}
MC/DC0%0/2
$self->is_allocated && $cache->{$self}{'format'}
$self->is_allocated
$cache->{$self}{'format'}
Truth table66%2/3
A && B
A= $self->is_allocatedB= $cache->{$self}{'format'}
| |||||||||||||||||||||||||||||
| 427 | 18 | $r = '+'.country_code().' '.substr(${$self}, 3) | |||||||||||||||||||||||||||
| 428 | } elsif($self->subscriber()) { # if there's a subscriber ... | ||||||||||||||||||||||||||||
| 429 | 0 | $r = '+'.country_code().' '.$self->subscriber | |||||||||||||||||||||||||||
| 430 | } | ||||||||||||||||||||||||||||
| 431 | 77 | return $r; | |||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | =item intra_country_dial_to | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | Within the UK numbering plan you can *always* dial 0xxxx xxxxxx | ||||||||||||||||||||||||||||
| 437 | for intra-country calls. In most places the leading 0$areacode is | ||||||||||||||||||||||||||||
| 438 | optional but in some it is required (see eg | ||||||||||||||||||||||||||||
| 439 | L<https://www.ofcom.org.uk/__data/assets/pdf_file/0017/19160/aberdeen_local_dialling_release.pdf>) and over time this | ||||||||||||||||||||||||||||
| 440 | will apply to more areas. | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | =cut | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | 12 | ▶ | sub intra_country_dial_to { | ||||||||||||||||||||||||||
|
Subroutine
intra_country_dial_to: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 445 | 12 | my $from = shift; | |||||||||||||||||||||||||||
| 446 | 12 | my $to = shift; | |||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | 12 | my $to_raw = $to->raw_number; | |||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | # for the full-fat implementation we don't want to say you can dial an | ||||||||||||||||||||||||||||
| 451 | # unallocated number, but if $to is a stub for whatever reason we can't | ||||||||||||||||||||||||||||
| 452 | # check that | ||||||||||||||||||||||||||||
| 453 | 12 | ▶ | die "$to_raw is not allocated\n" if( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$to->isa("Number::Phone::StubCountry") or $to->is_allocated
MC/DC100%2/2
$to->isa("Number::Phone::StubCountry") or $to->is_allocated
$to->isa("Number::Phone::StubCountry")
$to->is_allocated
Truth table100%3/3
A or B
A= $to->isa("Number::Phone::StubCountry")B= $to->is_allocated
| |||||||||||||||||||||||||||||
| 454 | !$to->isa('Number::Phone::StubCountry') && | ||||||||||||||||||||||||||||
| 455 | !$to->is_allocated() | ||||||||||||||||||||||||||||
| 456 | ); | ||||||||||||||||||||||||||||
| 457 | 11 | return "0$to_raw"; | |||||||||||||||||||||||||||
| 458 | } | ||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | =item country | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | If the number is_international, return the two-letter ISO country code. | ||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | NYI | ||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | =back | ||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | =head1 LIMITATIONS/BUGS/FEEDBACK | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | The results are only as up-to-date as the data included from OFCOM's | ||||||||||||||||||||||||||||
| 471 | official documentation of number range allocations. | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | No attempt is made to deal with number portability. | ||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | Please report bugs at L<https://github.com/DrHyde/perl-modules-Number-Phone/issues>, including, if possible, a test case. | ||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | I welcome feedback from users. | ||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||
| 479 | =head1 LICENCE | ||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||
| 481 | You may use, modify and distribute this software under the same terms as | ||||||||||||||||||||||||||||
| 482 | perl itself. | ||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||
| 484 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | David Cantrell E<lt>david@cantrell.org.ukE<gt> | ||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||
| 488 | Copyright 2026 | ||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | =cut | ||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||
| 492 | 16 | 1; | |||||||||||||||||||||||||||