line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Phone::Number; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
56560
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
23
|
|
4
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
48
|
|
5
|
1
|
|
33
|
1
|
|
560
|
no if $^V ge v5.18.0 && $^V lt v5.19.0, warnings => "experimental"; |
|
1
|
|
|
|
|
11
|
|
|
1
|
|
|
|
|
21
|
|
6
|
1
|
|
|
1
|
|
36
|
use 5.10.0; |
|
1
|
|
|
|
|
3
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 NAME |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
Phone::Number - Module to hold a phone number from a UK-centric |
11
|
|
|
|
|
|
|
point of view. |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
head1 VERSION |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
Version v1.1.2 |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=cut |
18
|
|
|
|
|
|
|
|
19
|
1
|
|
|
1
|
|
355
|
use version 0.77; our $VERSION = qv('v1.1.2'); |
|
1
|
|
|
|
|
1638
|
|
|
1
|
|
|
|
|
5
|
|
20
|
|
|
|
|
|
|
|
21
|
1
|
|
|
1
|
|
94
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
74
|
|
22
|
|
|
|
|
|
|
|
23
|
1
|
|
|
1
|
|
415
|
use experimental qw(switch); |
|
1
|
|
|
|
|
957
|
|
|
1
|
|
|
|
|
4
|
|
24
|
1
|
|
|
1
|
|
1137
|
use overload q("") => 'formatted'; |
|
1
|
|
|
|
|
784
|
|
|
1
|
|
|
|
|
5
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head1 SYNOPSYS |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
use Phone::Number; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
my $num = new Phone::Number('02002221666'); |
31
|
|
|
|
|
|
|
print $num->formatted; # 020 0222 1666 |
32
|
|
|
|
|
|
|
print $num->packed; # 02002221666 |
33
|
|
|
|
|
|
|
print $num->number; # +442002221666 |
34
|
|
|
|
|
|
|
print $num->plain; # 442002221666 |
35
|
|
|
|
|
|
|
print $num->uk ? "yes" : "no"; # yes |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=head1 EXPORT |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
Nothing is exported |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head1 ROUTINES |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head2 new |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
Creates a new, immutable object using any unambiguous phone |
46
|
|
|
|
|
|
|
number format. |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
my $num = new Phone::Number('02002221666'); |
49
|
|
|
|
|
|
|
my $num = new Phone::Number('2002221666'); |
50
|
|
|
|
|
|
|
my $num = new Phone::Number('442002221666'); |
51
|
|
|
|
|
|
|
my $num = new Phone::Number('+442002221666'); |
52
|
|
|
|
|
|
|
my $new = new Phone::Number($num); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=cut |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# Passed a string or a Number object. If the latter, simply returns it. |
57
|
|
|
|
|
|
|
sub new |
58
|
|
|
|
|
|
|
{ |
59
|
1
|
|
|
1
|
1
|
73
|
my $class = shift; |
60
|
1
|
50
|
|
|
|
5
|
my $number = shift or croak "No number passed to new $class"; |
61
|
1
|
50
|
33
|
|
|
4
|
return $number if ref $number && $number->isa($class); |
62
|
1
|
|
|
|
|
7
|
$number =~ s/^\s*(.*?)\s*$/$1/; # trim leading/trailing spaces |
63
|
1
|
|
|
|
|
3
|
$number =~ s/\D//g; # throw away non-digits |
64
|
1
|
|
|
|
|
2
|
$number =~ s/^44/0/; # change leading 44 into 0 |
65
|
1
|
|
|
|
|
2
|
$number =~ s/^(?=[1-9])/00/; # it still starts with a 1-9, add 00 |
66
|
1
|
|
|
|
|
2
|
my $self = {}; |
67
|
1
|
|
|
|
|
3
|
$self->{raw} = $number; |
68
|
1
|
|
|
|
|
4
|
$self->{valid} = $number =~ /^0[123578]\d{8,9}$/; |
69
|
1
|
|
|
|
|
2
|
my $formatted; |
70
|
1
|
|
|
|
|
1
|
given ($number) |
71
|
|
|
|
|
|
|
{ |
72
|
|
|
|
|
|
|
when (/^02/) |
73
|
1
|
|
|
|
|
4
|
{ |
74
|
1
|
|
|
|
|
11
|
($formatted = $number) =~ s/^(\d{3})(\d{4})(\d*)/$1 $2 $3/; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
when (/^03/) |
77
|
0
|
|
|
|
|
0
|
{ |
78
|
0
|
|
|
|
|
0
|
($formatted = $number) =~ s/^(\d{4})(\d{3})(\d*)/$1 $2 $3/; |
79
|
|
|
|
|
|
|
} |
80
|
0
|
|
0
|
|
|
0
|
when (/^01\d?1/ || /^08[47]/) { |
81
|
0
|
|
|
|
|
0
|
($formatted = $number) =~ s/^(\d{4})(\d{3})(\d*)/$1 $2 $3/; |
82
|
|
|
|
|
|
|
} |
83
|
0
|
|
|
|
|
0
|
when (/^0[85]0/) { |
84
|
0
|
|
|
|
|
0
|
($formatted = $number) =~ s/^(\d{4})(\d{3})(\d*)/$1 $2 $3/; |
85
|
|
|
|
|
|
|
} |
86
|
0
|
|
|
|
|
0
|
when (/^0(?!0)/) { |
87
|
0
|
|
|
|
|
0
|
($formatted = $number) =~ s/^(\d{5})(\d*)/$1 $2/; |
88
|
|
|
|
|
|
|
} |
89
|
0
|
|
|
|
|
0
|
default { |
90
|
0
|
|
|
|
|
0
|
$formatted = $number; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
} |
93
|
1
|
|
|
|
|
3
|
$self->{formatted} = $formatted; |
94
|
1
|
|
|
|
|
2
|
$number =~ s/^00/+/; |
95
|
1
|
|
|
|
|
3
|
$number =~ s/^0/+44/; |
96
|
1
|
|
|
|
|
2
|
$self->{number} = $number; |
97
|
1
|
|
|
|
|
13
|
bless $self, $class; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head2 formatted |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
Returns the number formatted with leading 0 and spaces. |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
This can be used for displaying the number in "standard" format. |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
The raw object stringifies to the formatted version. |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=cut |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub formatted |
111
|
|
|
|
|
|
|
{ |
112
|
3
|
|
|
3
|
1
|
10
|
my $self = shift; |
113
|
3
|
|
|
|
|
46
|
return $self->{formatted}; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head2 packed |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
Returns the number with a leading 0 but no spaces. |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
This can be useful for databases but see L below. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=cut |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub packed |
125
|
|
|
|
|
|
|
{ |
126
|
1
|
|
|
1
|
1
|
3
|
my $self = shift; |
127
|
1
|
|
|
|
|
3
|
(my $packed = $self->formatted) =~ s/\s+//g; |
128
|
1
|
|
|
|
|
4
|
return $packed; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head2 number |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
Returns the number in international format starting with +. |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=cut |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub number |
138
|
|
|
|
|
|
|
{ |
139
|
1
|
|
|
1
|
1
|
2
|
my $self = shift; |
140
|
1
|
|
|
|
|
4
|
return $self->{number}; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head2 plain |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
Returns the number in international format without the +. |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
This is usually the best way to store the number onto a database. |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=cut |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
sub plain |
152
|
|
|
|
|
|
|
{ |
153
|
1
|
|
|
1
|
1
|
3
|
my $self = shift; |
154
|
1
|
|
|
|
|
9
|
(my $plain = $self->{number}) =~ s/^\+//; |
155
|
1
|
|
|
|
|
6
|
return $plain; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head2 uk |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
Returns a boolean: true if it is a valid UK number |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=cut |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub uk |
165
|
|
|
|
|
|
|
{ |
166
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
167
|
0
|
|
|
|
|
|
return $self->{valid}; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head1 AUTHOR |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
Cliff Stanford, C<< >> |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=head1 BUGS |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
Please report any bugs or feature requests to |
177
|
|
|
|
|
|
|
C, or through |
178
|
|
|
|
|
|
|
the web interface at |
179
|
|
|
|
|
|
|
L. |
180
|
|
|
|
|
|
|
I will be notified, and then you'll |
181
|
|
|
|
|
|
|
automatically be notified of progress on your bug as I make changes. |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=head1 SUPPORT |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
perldoc Phone::Number |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
Copyright 2014 Cliff Stanford. |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
194
|
|
|
|
|
|
|
under the same terms as Perl itself. |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=cut |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
1; |