blib/lib/Number/Phone/US.pm

filter: Statementstmt 94.7% 18 / 19 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 3 / 4 Podpod 100% 2 / 2 totaltotal 92.5% 25 / 27 CC 2 SCAR 4.7
CC
2
Cov
95%
validate_number
2.0
get_regex
2.0
BEGIN
1.0
CRAP 1.6
↑ Summary
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 1 require 5;
50  
51 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 ▶use vars qw($VERSION @EXPORT_OK %EXPORT_TAGS @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 1 require Exporter;
55  
56 1 @ISA = qw(Exporter);
57  
58 1 $VERSION = (split / /, q$Id: US.pm,v 1.5 2002/08/10 05:13:25 kennedyh Exp $ )[2];
59  
60 1 %EXPORT_TAGS = ( );
61 1 @EXPORT_OK = qw(&validate_number &is_valid_number &get_regex);
62  
63 #prototypes
64  
65 sub validate_number($ );
66 sub get_regex();
67  
68 1 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 19 ▶sub validate_number ($ ) {
Subroutine validate_number: called CC=2 SCAR=6.9
Pod documented
91 19 my ($number) = @_;
92  
93 19 ▶ return 1 if $number =~ /$ROUGH_MATCH_STRING/o;
Branch: 1 branch
BranchTrueFalseTotal
if $number =~ /$ROUGH_MATCH_STRING/o 17 2 19
94 2 return 0;
95  
96 }
97  
98 # *yaay* aliasing
99 1 *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 0 ▶sub get_regex () {
Subroutine get_regex: not called CC=1 SCAR=6.9
Pod documented
121 0 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 <kennedyh@engin.umich.edu>
161  
162 __| \ __| \ |
163 ( _ \ _| . |
164 \___|_/ _\___|_|\_|
165  
166 =cut
167  
168  
169 1 'utterly false';
↑ Summary