lib/Parse/PhoneNumber.pm

filter: Statementstmt 92.0% 69 / 75 Branchbran 72.7% 16 / 22 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 94.1% 16 / 17 Podpod 75.0% 3 / 4 totaltotal 88.6% 109 / 123 CC 14 SCAR 19.1
CC
14
Cov
88%
parse
14.0
errstr
2.0
clear_errstr
2.0
CRAP 6.8
↑ Summary
1 package Parse::PhoneNumber;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use vars qw[$VERSION $EXT $MINLEN $MIN_US_LENGTH @CCODES];
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 $VERSION = qw(1.9);
10 2 $EXT = qr/\s*(?:(?:ext|ex|xt|x)[\s.:]*(\d+))/i;
11  
12 2 $MINLEN = 7;
13 2 $MIN_US_LENGTH = 10;
14  
15 2 @CCODES = qw[
16 1 7 20 27 30 31 32 33 34
17 36 39 40 41 43 44 45 46 47
18 48 49 51 52 53 54 55 56 57
19 58 60 61 62 63 64 65 66 81
20 82 84 86 90 91 92 93 94 95
21 98 212 213 216 218 220 221 222 223
22 224 225 226 227 228 229 230 231 232
23 233 234 235 236 237 238 239 240 241
24 242 243 244 245 246 247 248 249 250
25 251 252 253 254 255 256 257 258 260
26 261 262 263 264 265 266 267 268 269
27 290 291 297 298 299 350 351 352 353
28 354 355 356 357 358 359 370 371 372
29 373 374 375 376 377 378 380 381 385
30 386 387 388 389 420 421 423 500 501
31 502 503 504 505 506 507 508 509 590
32 591 592 593 594 595 596 597 598 599
33 670 672 673 674 675 676 677 678 679
34 680 681 682 683 684 685 686 687 688
35 689 690 691 692 800 808 850 852 853
36 655 856 870 871 872 873 874 878 880
37 881 882 886 960 961 962 963 964 965
38 966 967 968 970 971 972 973 974 975
39 976 977 979 991 992 993 994 995 996
40 998
41 ];
42  
43 =head1 NAME
44  
45 Parse::PhoneNumber - Parse Phone Numbers
46  
47 =head1 SYNOPSIS
48  
49 use Parse::PhoneNumber;
50 my $number = Parse::PhoneNumber->parse( number => $phone );
51
52 print $number->human;
53  
54 =head1 ABSTRACT
55  
56 Parse phone numbers. Phone number have a defined syntax (to a point),
57 so they can be parsed (to a point).
58  
59 =head1 DESCRIPTION
60  
61 =head2 Methods
62  
63 =head3 new
64  
65 Create a new Parse::PhoneNumber object. Useful if a lot of numbers
66 have to be parsed.
67  
68 =cut
69  
70 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
71 2 return bless {}, shift;
72 }
73  
74 =head3 parse
75  
76 Accepts a list of arguments. C<number> is the phone number. This method
77 will return C<undef> and set C<errstr> on failure. On success, a
78 C<Parse::PhoneNumber::Number> object is returned. C<assume_us> will have
79 the country code default to C<1> if none is given. This is due to the fact
80 that most people in the US are clueless about such things.
81  
82 =cut
83  
84 110 ▶sub parse {
Subroutine parse: called CC=13 SCAR=26.4
Pod documented
85 110 my ($class, %data) = @_;
86 110 ▶ croak "No phone number" unless $data{number};
Branch: 1 branch
BranchTrueFalseTotal
unless $data{"number"} 0 110 110
87  
88 110 local $_ = $data{number};
89 110 s/^\s+//;s/\s+$//;
90  
91 my %number = (
92 orig => $data{number},
93 110 cc => undef,
94 num => undef,
95 ext => undef,
96 opensrs => undef,
97 human => undef,
98 );
99
100
101  
102 110 ▶ if ( m/$EXT$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/$Parse::PhoneNumber::EXT$/) 0 110 110
103 0 ▶ if ( length $1 > 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $1 > 4) { } 0 0 0
104 0 $class->errstr( "Extension '$1' longer than four digits" );
105 0 return undef;
106 } else {
107 0 $number{ext} = $1;
108 0 s/$EXT$//;
109 }
110 }
111
112 110 s/\D//g;
113 110 s/^0+//;
114  
115 110 ▶ if ($data{'assume_us'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data{'assume_us'}) { } 13 97 110
116 13 ▶ if (length $_ < $MIN_US_LENGTH) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ < $Parse::PhoneNumber::MIN_US_LENGTH) { } 7 6 13
117 7 $class->errstr("Invalid US number: $data{number}" );
118 7 return;
119 } else {
120 6 $number{'cc'} = 1;
121 6 s/^1//;
122 6 $number{'num'} = $_;
123 }
124 } else {
125
126 97 foreach my $len ( 1 .. 3 ) {
127 282 ▶ last if $number{cc};
Branch: 1 branch
BranchTrueFalseTotal
if $number{"cc"} 53 229 282
128
129 229 my $cc = substr $_, 0, $len;
130
131 229 ▶ if ( grep { $_ eq $cc } @CCODES ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $cc;} @Parse::PhoneNumber::CCODES) 92 137 229
132 92 $number{cc} = $cc;
133 92 s/^$cc//;
134 }
135 }
136
137 97 ▶ if ( $number{cc} && length "$number{cc}$_" >= $MINLEN ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number{'cc'} and length "$number{'cc'}$_" >= $Parse::PhoneNumber::MINLEN) { } 91 6 97
Condition100%3/3
$number{"cc"} and length qq[$number{"cc"}$_] >= $Parse::PhoneNumber::MINLEN
!ll&&!rl&&r
5191
MC/DC100%2/2
$number{"cc"} and length qq[$number{"cc"}$_] >= $Parse::PhoneNumber::MINLEN
$number{"cc"} length qq[$number{"cc"}$_] >= $Parse::PhoneNumber::MINLEN
Truth table100%3/3
A and B
A= $number{"cc"}B= length qq[$number{"cc"}$_] >= $Parse::PhoneNumber::MINLEN
ABresult
0X0
100
111
138 91 $number{num} = "$_";
139 } else {
140 6 $class->errstr("Invalid international number: $data{number}" );
141 6 return undef;
142 }
143 }
144
145 97 $number{opensrs} = sprintf "+%d.%s", @number{qw[cc num]};
146 97 ▶ $number{opensrs} .= sprintf "x%d", $number{ext} if $number{ext};
Branch: 1 branch
BranchTrueFalseTotal
if $number{"ext"} 0 97 97
147
148 97 $number{human} = sprintf "+%d %s", @number{qw[cc num]};
149 97 ▶ $number{human} .= sprintf " x%d", $number{ext} if $number{ext};
Branch: 1 branch
BranchTrueFalseTotal
if $number{"ext"} 0 97 97
150
151 97 return Parse::PhoneNumber::Number->new( %number );
152 }
153  
154 =head3 errstr
155  
156 Returns the last error reported, or undef if no errors have occured yet.
157  
158 =cut
159  
160 {
161 2 my $errstr = undef;
162 25 ▶ sub errstr { $errstr = $_[1] if $_[1]; $errstr }
Subroutine errstr: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] 13 12 25
163 0 ▶ sub clear_errstr { $errstr = undef; }
Subroutine clear_errstr: not called CC=1 SCAR=6.9
Pod undocumented
164 }
165  
166 2 package Parse::PhoneNumber::Number;
167 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
168 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
169  
170 =head2 Parse::PhoneNumber::Number Objects
171  
172 The objects returned on a successful parse.
173  
174 =cut
175  
176 97 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
177 97 my ($class, %data) = @_;
178 97 return bless \%data, $class;
179 }
180  
181 =head3 orig
182  
183 The original string passed to C<parse>.
184  
185 =head3 cc
186  
187 The Country Code
188  
189 =head3 num
190  
191 The phone number, including the trunk pointer, area code, and
192 subscriber number.
193  
194 =head3 ext
195  
196 An extension, if one is present.
197  
198 =head3 opensrs
199  
200 The format an OpenSRS Registrar must make a phone number for some
201 TLDs.
202  
203 =head3 human
204  
205 Human readable format.
206  
207 =cut
208  
209 194 ▶sub orig { $_[0]->{orig} }
Subroutine orig: called CC=1 SCAR=0.0
210 194 ▶sub cc { $_[0]->{cc} }
Subroutine cc: called CC=1 SCAR=0.0
211 97 ▶sub num { $_[0]->{num} }
Subroutine num: called CC=1 SCAR=0.0
212 194 ▶sub ext { $_[0]->{ext} }
Subroutine ext: called CC=1 SCAR=0.0
213 194 ▶sub opensrs { $_[0]->{opensrs} }
Subroutine opensrs: called CC=1 SCAR=0.0
214 194 ▶sub human { $_[0]->{human} }
Subroutine human: called CC=1 SCAR=0.0
215  
216 2 1;
217  
218 __END__
↑ Summary