blib/lib/Phone/Number.pm

filter: Statementstmt 81.8% 54 / 66 Branchbran 50.0% 2 / 4 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 92.8% 13 / 14 Podpod 100% 6 / 6 totaltotal 73.3% 77 / 105 CC 6 SCAR 10.2
CC
6
Cov
73%
new
7.1
BEGIN
2.1
uk
2.0
CRAP 2.8
↑ Summary
1 package Phone::Number;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶no if $^V ge v5.18.0 && $^V lt v5.19.0, warnings => "experimental";
Subroutine BEGIN: called CC=2 SCAR=7.6
Condition33%1/3
$^V ge v5.18.0 && $^V lt v5.19.0
!ll&&!rl&&r
010
MC/DC0%0/2
$^V ge v5.18.0 && $^V lt v5.19.0
$^V ge v5.18.0 $^V lt v5.19.0
Truth table33%1/3
A && B
A= $^V ge v5.18.0B= $^V lt v5.19.0
ABresult
0X0
100
111
6 1 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 ▶use version 0.77; our $VERSION = qv('v1.1.2');
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶use experimental qw(switch);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use overload q("") => 'formatted';
Subroutine BEGIN: called CC=1 SCAR=0.0
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 1 ▶{
Subroutine new: called CC=5 SCAR=19.6
Pod documented
59 1 my $class = shift;
60 1 ▶ my $number = shift or croak "No number passed to new $class";
Branch: 1 branch
BranchTrueFalseTotal
my $number = shift() or croak("No number passed to new $class") 1 0 1
61 1 ▶ return $number if ref $number && $number->isa($class);
Branch: 1 branch
BranchTrueFalseTotal
if ref $number and $number->isa($class) 0 1 1
Condition33%1/3
ref $number and $number->isa($class)
!ll&&!rl&&r
100
MC/DC0%0/2
ref $number and $number->isa($class)
ref $number $number->isa($class)
Truth table33%1/3
A and B
A= ref $numberB= $number->isa($class)
ABresult
0X0
100
111
62 1 $number =~ s/^\s*(.*?)\s*$/$1/; # trim leading/trailing spaces
63 1 $number =~ s/\D//g; # throw away non-digits
64 1 $number =~ s/^44/0/; # change leading 44 into 0
65 1 $number =~ s/^(?=[1-9])/00/; # it still starts with a 1-9, add 00
66 1 my $self = {};
67 1 $self->{raw} = $number;
68 1 $self->{valid} = $number =~ /^0[123578]\d{8,9}$/;
69 1 my $formatted;
70 1 given ($number)
71 {
72 when (/^02/)
73 1 {
74 1 ($formatted = $number) =~ s/^(\d{3})(\d{4})(\d*)/$1 $2 $3/;
75 }
76 when (/^03/)
77 0 {
78 0 ($formatted = $number) =~ s/^(\d{4})(\d{3})(\d*)/$1 $2 $3/;
79 }
80 0 ▶ when (/^01\d?1/ || /^08[47]/) {
Condition0%0/3
/^01\d?1/ || /^08[47]/
l!l&&r!l&&!r
000
MC/DC0%0/2
/^01\d?1/ || /^08[47]/
/^01\d?1/ /^08[47]/
Truth table0%0/3
A || B
A= /^01\d?1/B= /^08[47]/
ABresult
1X1
011
000
81 0 ($formatted = $number) =~ s/^(\d{4})(\d{3})(\d*)/$1 $2 $3/;
82 }
83 0 when (/^0[85]0/) {
84 0 ($formatted = $number) =~ s/^(\d{4})(\d{3})(\d*)/$1 $2 $3/;
85 }
86 0 when (/^0(?!0)/) {
87 0 ($formatted = $number) =~ s/^(\d{5})(\d*)/$1 $2/;
88 }
89 0 default {
90 0 $formatted = $number;
91 }
92 }
93 1 $self->{formatted} = $formatted;
94 1 $number =~ s/^00/+/;
95 1 $number =~ s/^0/+44/;
96 1 $self->{number} = $number;
97 1 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 3 ▶{
Subroutine formatted: called CC=1 SCAR=0.0
Pod documented
112 3 my $self = shift;
113 3 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</plain> below.
121  
122 =cut
123  
124 sub packed
125 1 ▶{
Subroutine packed: called CC=1 SCAR=0.0
Pod documented
126 1 my $self = shift;
127 1 (my $packed = $self->formatted) =~ s/\s+//g;
128 1 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 1 ▶{
Subroutine number: called CC=1 SCAR=0.0
Pod documented
139 1 my $self = shift;
140 1 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 1 ▶{
Subroutine plain: called CC=1 SCAR=0.0
Pod documented
153 1 my $self = shift;
154 1 (my $plain = $self->{number}) =~ s/^\+//;
155 1 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 0 ▶{
Subroutine uk: not called CC=1 SCAR=6.9
Pod documented
166 0 my $self = shift;
167 0 return $self->{valid};
168 }
169  
170 =head1 AUTHOR
171  
172 Cliff Stanford, C<< <cpan@may.be> >>
173  
174 =head1 BUGS
175  
176 Please report any bugs or feature requests to
177 C<bug-phone-number at rt.cpan.org>, or through
178 the web interface at
179 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Phone-Number>.
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 1;
↑ Summary