line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# $Id: US.pm,v 1.5 2002/08/10 05:13:25 kennedyh Exp $ |
2
|
|
|
|
|
|
|
=pod |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
=head1 NAME |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
Number::Phone::US - Validate US-style phone numbers |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
=head1 SYNOPSIS |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use Number::Phone::US qw(is_valid_number); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
&do_that_thing if is_valid_number($input); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 DESCRIPTION |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
Number::Phone::US is a simple module to validate US-sytle phone number formats. |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
Currently marks as valid, phone numbers of the following forms: |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
(734) 555 1212 |
21
|
|
|
|
|
|
|
(734) 555.1212 |
22
|
|
|
|
|
|
|
(734) 555-1212 |
23
|
|
|
|
|
|
|
(734) 5551212 |
24
|
|
|
|
|
|
|
(734)5551212 |
25
|
|
|
|
|
|
|
734 555 1212 |
26
|
|
|
|
|
|
|
734.555.1212 |
27
|
|
|
|
|
|
|
734-555-1212 |
28
|
|
|
|
|
|
|
7345551212 |
29
|
|
|
|
|
|
|
555 1212 |
30
|
|
|
|
|
|
|
555.1212 |
31
|
|
|
|
|
|
|
555-1212 |
32
|
|
|
|
|
|
|
5551212 |
33
|
|
|
|
|
|
|
5 1212 |
34
|
|
|
|
|
|
|
5.1212 |
35
|
|
|
|
|
|
|
5-1212 |
36
|
|
|
|
|
|
|
51212 |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
Currently marks as invalid, phone numbers of the following forms: |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
734-555.1212 |
41
|
|
|
|
|
|
|
734-5551212 |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=over 8 |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=cut |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
package Number::Phone::US; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
require 5; |
50
|
|
|
|
|
|
|
|
51
|
1
|
|
|
1
|
|
16089
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
60
|
|
52
|
1
|
|
|
1
|
|
6
|
use vars qw($VERSION @EXPORT_OK %EXPORT_TAGS @ISA); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
292
|
|
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
require Exporter; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
$VERSION = (split / /, q$Id: US.pm,v 1.5 2002/08/10 05:13:25 kennedyh Exp $ )[2]; |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
%EXPORT_TAGS = ( ); |
61
|
|
|
|
|
|
|
@EXPORT_OK = qw(&validate_number &is_valid_number &get_regex); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
#prototypes |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub validate_number($ ); |
66
|
|
|
|
|
|
|
sub get_regex(); |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
my $ROUGH_MATCH_STRING = q{^\d\d\d([-.\s]?)\d\d\d\1\d\d\d\d$|^(:?(:?\(\d\d\d\))?\s*\d\d)?\d[-.\s]?\d\d\d\d$}; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
############ |
71
|
|
|
|
|
|
|
# |
72
|
|
|
|
|
|
|
=pod |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=item validate_number($ ) |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
Use like |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
if ( validate_number($number) ) { &foo; } |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
returns true if $number is a properly formatted US phone number. |
81
|
|
|
|
|
|
|
does _not_ check and see if $number is a functioning number, although |
82
|
|
|
|
|
|
|
maybe it should. |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
this function can also be called as: is_valid_number($number) |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=cut |
87
|
|
|
|
|
|
|
# |
88
|
|
|
|
|
|
|
############ |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub validate_number ($ ) { |
91
|
19
|
|
|
19
|
1
|
8305
|
my ($number) = @_; |
92
|
|
|
|
|
|
|
|
93
|
19
|
100
|
|
|
|
280
|
return 1 if $number =~ /$ROUGH_MATCH_STRING/o; |
94
|
2
|
|
|
|
|
9
|
return 0; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# *yaay* aliasing |
99
|
|
|
|
|
|
|
*is_valid_number = \&validate_number; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
############ |
103
|
|
|
|
|
|
|
# |
104
|
|
|
|
|
|
|
=pod |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=item get_regex () |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Use like |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
$rough_regex = get_regex; |
111
|
|
|
|
|
|
|
if ( $phone =~ /$rough_regex/o ) { &foo } |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
returns the rough regex string (does not enforce |
114
|
|
|
|
|
|
|
phone number consistency.) |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=cut |
117
|
|
|
|
|
|
|
# |
118
|
|
|
|
|
|
|
############ |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub get_regex () { |
121
|
0
|
|
|
0
|
1
|
|
return $ROUGH_MATCH_STRING; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=pod |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=back |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head1 COPYRIGHT |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
COPYRIGHT 2000 THE REGENTS OF THE UNIVERSITY OF MICHIGAN |
131
|
|
|
|
|
|
|
ALL RIGHTS RESERVED |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
PERMISSION IS GRANTED TO USE, COPY, CREATE DERIVATIVE WORKS |
134
|
|
|
|
|
|
|
AND REDISTRIBUTE THIS SOFTWARE AND SUCH DERIVATIVE WORKS FOR |
135
|
|
|
|
|
|
|
NON-COMMERCIAL EDUCATION AND RESEARCH PURPOSES, SO LONG AS NO |
136
|
|
|
|
|
|
|
FEE IS CHARGED, AND SO LONG AS THE COPYRIGHT NOTICE ABOVE, |
137
|
|
|
|
|
|
|
THIS GRANT OF PERMISSION, AND THE DISCLAIMER BELOW APPEAR IN |
138
|
|
|
|
|
|
|
ALL COPIES MADE; AND SO LONG AS THE NAME OF THE UNIVERSITY |
139
|
|
|
|
|
|
|
OF MICHIGAN IS NOT USED IN ANY ADVERTISING OR PUBLICITY |
140
|
|
|
|
|
|
|
PERTAINING TO THE USE OR DISTRIBUTION OF THIS SOFTWARE |
141
|
|
|
|
|
|
|
WITHOUT SPECIFIC, WRITTEN PRIOR AUTHORIZATION. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION AS |
144
|
|
|
|
|
|
|
TO ITS FITNESS FOR ANY PURPOSE, AND WITHOUT WARRANTY OF ANY |
145
|
|
|
|
|
|
|
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT |
146
|
|
|
|
|
|
|
LIMITATION THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
147
|
|
|
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. THE REGENTS OF THE |
148
|
|
|
|
|
|
|
UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE FOR ANY DAMAGES, |
149
|
|
|
|
|
|
|
INCLUDING SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL |
150
|
|
|
|
|
|
|
DAMAGES, WITH RESPECT TO ANY CLAIM ARISING OUT OF OR IN |
151
|
|
|
|
|
|
|
CONNECTION WITH THE USE OF THE SOFTWARE, EVEN IF IT HAS BEEN |
152
|
|
|
|
|
|
|
OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head1 SEE ALSO |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
perl(1) |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head1 AUTHOR |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
Hugh Kennedy |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
__| \ __| \ | |
163
|
|
|
|
|
|
|
( _ \ _| . | |
164
|
|
|
|
|
|
|
\___|_/ _\___|_|\_| |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=cut |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
'utterly false'; |