Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit of Parse::PhoneNumber 1.6 from CPAN sources

  • Loading branch information...
commit 622fc8be1e4e70457e20c1bcf8f445ac81ff5ead 0 parents
Tim Wilde authored
14 Build.PL
@@ -0,0 +1,14 @@
+# $Id: Build.PL,v 1.3 2005/03/15 00:17:30 twilde Exp $
+use strict;
+
+use Module::Build;
+
+Module::Build->new(
+ module_name => 'Parse::PhoneNumber',
+ license => 'perl',
+ build_requires => {
+ 'Test::More' => 0,
+ 'Module::Build' => 0,
+ },
+ create_makefile_pl => 'passthrough',
+)->create_build_script;
6 INSTALL
@@ -0,0 +1,6 @@
+#!/bin/sh
+# $Id: INSTALL,v 1.1 2003/03/26 22:18:03 cwest Exp $
+perl Build.PL
+./Build
+./Build test
+./Build install
13 MANIFEST
@@ -0,0 +1,13 @@
+# $Id: MANIFEST,v 1.3 2005/03/15 00:12:20 twilde Exp $
+Build.PL
+INSTALL
+lib/Parse/PhoneNumber.pm
+Makefile.PL
+MANIFEST
+META.yml
+README
+t/bad_numbers.txt
+t/good_numbers.txt
+t/numbers.t
+t/pod.t
+t/us_numbers.t
20 META.yml
@@ -0,0 +1,20 @@
+---
+name: Parse-PhoneNumber
+version: 1.6
+author:
+ - |-
+ Casey West <F<casey@geeknest.com>>
+ Maintained by Tim Wilde <F<twilde@dyndns.com>>
+abstract: Parse Phone Numbers
+license: perl
+build_requires:
+ Module::Build: 0
+ Test::More: 0
+provides:
+ Parse::PhoneNumber:
+ file: lib/Parse/PhoneNumber.pm
+ version: 1.6
+ Parse::PhoneNumber::Number:
+ file: lib/Parse/PhoneNumber.pm
+ version: 1.6
+generated_by: Module::Build version 0.25
31 Makefile.PL
@@ -0,0 +1,31 @@
+# Note: this file was auto-generated by Module::Build::Compat version 0.03
+
+ unless (eval "use Module::Build::Compat 0.02; 1" ) {
+ print "This module requires Module::Build to install itself.\n";
+
+ require ExtUtils::MakeMaker;
+ my $yn = ExtUtils::MakeMaker::prompt
+ (' Install Module::Build now from CPAN?', 'y');
+
+ unless ($yn =~ /^y/i) {
+ die " *** Cannot install without Module::Build. Exiting ...\n";
+ }
+
+ require Cwd;
+ require File::Spec;
+ require CPAN;
+
+ # Save this 'cause CPAN will chdir all over the place.
+ my $cwd = Cwd::cwd();
+ my $makefile = File::Spec->rel2abs($0);
+
+ CPAN::Shell->install('Module::Build::Compat')
+ or die " *** Cannot install without Module::Build. Exiting ...\n";
+
+ chdir $cwd or die "Cannot chdir() back to $cwd: $!";
+ exec $^X, $makefile, @ARGV; # Redo now that we have Module::Build
+ }
+ use lib '_build/lib';
+ Module::Build::Compat->run_build_pl(args => \@ARGV);
+ require Module::Build;
+ Module::Build::Compat->write_makefile(build_class => 'Module::Build');
68 README
@@ -0,0 +1,68 @@
+NAME
+ Parse::PhoneNumber - Parse Phone Numbers
+
+SYNOPSIS
+ use Parse::PhoneNumber;
+ my $number = Parse::PhoneNumber->parse( number => $phone );
+
+ print $number->human;
+
+ABSTRACT
+ Parse phone numbers. Phone number have a defined syntax (to a point), so
+ they can be parsed (to a point).
+
+DESCRIPTION
+ Methods
+ new
+ Create a new Parse::PhoneNumber object. Useful if a lot of numbers have
+ to be parsed.
+
+ parse
+ Accepts a list of arguments. "number" is the phone number. This method
+ will return "undef" and set "errstr" on failure. On success, a
+ "Parse::PhoneNumber::Number" object is returned. "assume_us" will have
+ the country code default to 1 if none is given. This is due to the fact
+ that most people in the US are clueless about such things.
+
+ errstr
+ Returns the last error reported, or undef if no errors have occured yet.
+
+ Parse::PhoneNumber::Number Objects
+ The objects returned on a successful parse.
+
+ orig
+ The original string passed to "parse".
+
+ cc
+ The Country Code
+
+ num
+ The phone number, including the trunk pointer, area code, and subscriber
+ number.
+
+ ext
+ An extension, if one is present.
+
+ opensrs
+ The format an OpenSRS Registrar must make a phone number for some TLDs.
+
+ human
+ Human readable format.
+
+BUGS
+ Currently only accept phone numbers in International format. If a number
+ isn't given in international format, a false positive could occur.
+
+AUTHOR
+ Casey West <casey@geeknest.com> Maintained by Tim Wilde
+ <twilde@dyndns.com>
+
+COPYRIGHT
+ Copyright (c) 2003 Casey West <casey@geeknest.com>. Portions Copyright
+ (c) 2005 Dynamic Network Services, Inc.
+
+ All rights reserved.
+
+ This program is free software; you can redistribute it and/or modify it
+ under the same terms as Perl itself.
+
239 lib/Parse/PhoneNumber.pm
@@ -0,0 +1,239 @@
+package Parse::PhoneNumber;
+# $Id: PhoneNumber.pm,v 1.8 2005/10/31 16:42:04 twilde Exp $
+use strict;
+$^W = 1;
+
+use Carp;
+
+use vars qw[$VERSION $EXT $MINLEN $MIN_US_LENGTH @CCODES];
+
+$VERSION = qw(1.6);
+$EXT = qr/\s*(?:(?:ext|ex|xt|x)[\s.:]*(\d+))/i;
+
+$MINLEN = 7;
+$MIN_US_LENGTH = 10;
+
+@CCODES = qw[
+ 1 7 20 27 30 31 32 33 34
+ 36 39 40 41 43 44 45 46 47
+ 48 49 51 52 53 54 55 56 57
+ 58 60 61 62 63 64 65 66 81
+ 82 84 86 90 91 92 93 94 95
+ 98 212 213 216 218 220 221 222 223
+ 224 225 226 227 228 229 230 231 232
+ 233 234 235 236 237 238 239 240 241
+ 242 243 244 245 246 247 248 249 250
+ 251 252 253 254 255 256 257 258 260
+ 261 262 263 264 265 266 267 268 269
+ 290 291 297 298 299 350 351 352 353
+ 354 355 356 357 358 359 370 371 372
+ 373 374 375 376 377 378 380 381 385
+ 386 387 388 389 420 421 423 500 501
+ 502 503 504 505 506 507 508 509 590
+ 591 592 593 594 595 596 597 598 599
+ 670 672 673 674 675 676 677 678 679
+ 680 681 682 683 684 685 686 687 688
+ 689 690 691 692 800 808 850 852 853
+ 655 856 870 871 872 873 874 878 880
+ 881 882 886 960 961 962 963 964 965
+ 966 967 968 970 971 972 973 974 975
+ 976 977 979 991 992 993 994 995 996
+ 998
+];
+
+=head1 NAME
+
+Parse::PhoneNumber - Parse Phone Numbers
+
+=head1 SYNOPSIS
+
+ use Parse::PhoneNumber;
+ my $number = Parse::PhoneNumber->parse( number => $phone );
+
+ print $number->human;
+
+=head1 ABSTRACT
+
+Parse phone numbers. Phone number have a defined syntax (to a point),
+so they can be parsed (to a point).
+
+=head1 DESCRIPTION
+
+=head2 Methods
+
+=head3 new
+
+Create a new Parse::PhoneNumber object. Useful if a lot of numbers
+have to be parsed.
+
+=cut
+
+sub new {
+ return bless {}, shift;
+}
+
+=head3 parse
+
+Accepts a list of arguments. C<number> is the phone number. This method
+will return C<undef> and set C<errstr> on failure. On success, a
+C<Parse::PhoneNumber::Number> object is returned. C<assume_us> will have
+the country code default to C<1> if none is given. This is due to the fact
+that most people in the US are clueless about such things.
+
+=cut
+
+sub parse {
+ my ($class, %data) = @_;
+ croak "No phone number" unless $data{number};
+
+ local $_ = $data{number};
+ s/^\s+//;s/\s+$//;
+
+ my %number = (
+ orig => $data{number},
+ cc => undef,
+ num => undef,
+ ext => undef,
+ opensrs => undef,
+ human => undef,
+ );
+
+
+
+ if ( m/$EXT$/ ) {
+ if ( length $1 > 4 ) {
+ $class->errstr( "Extension '$1' longer than four digits" );
+ return undef;
+ } else {
+ $number{ext} = $1;
+ s/$EXT$//;
+ }
+ }
+
+ s/\D//g;
+ s/^0+//;
+
+ if ($data{'assume_us'}) {
+ if (length $_ < $MIN_US_LENGTH) {
+ $class->errstr("Invalid US number: $data{number}" );
+ return;
+ } else {
+ $number{'cc'} = 1;
+ s/^1//;
+ $number{'num'} = $_;
+ }
+ } else {
+
+ foreach my $len ( 1 .. 3 ) {
+ last if $number{cc};
+
+ my $cc = substr $_, 0, $len;
+
+ if ( grep { $_ eq $cc } @CCODES ) {
+ $number{cc} = $cc;
+ s/^$cc//;
+ }
+ }
+
+ if ( $number{cc} && length "$number{cc}$_" >= $MINLEN ) {
+ $number{num} = "$_";
+ } else {
+ $class->errstr("Invalid international number: $data{number}" );
+ return undef;
+ }
+ }
+
+ $number{opensrs} = sprintf "+%d.%s", @number{qw[cc num]};
+ $number{opensrs} .= sprintf "x%d", $number{ext} if $number{ext};
+
+ $number{human} = sprintf "+%d %s", @number{qw[cc num]};
+ $number{human} .= sprintf " x%d", $number{ext} if $number{ext};
+
+ return Parse::PhoneNumber::Number->new( %number );
+}
+
+=head3 errstr
+
+Returns the last error reported, or undef if no errors have occured yet.
+
+=cut
+
+{
+ my $errstr = undef;
+ sub errstr { $errstr = $_[1] if $_[1]; $errstr }
+}
+
+package Parse::PhoneNumber::Number;
+use strict;
+$^W = 1;
+
+=head2 Parse::PhoneNumber::Number Objects
+
+The objects returned on a successful parse.
+
+=cut
+
+sub new {
+ my ($class, %data) = @_;
+ return bless \%data, $class;
+}
+
+=head3 orig
+
+The original string passed to C<parse>.
+
+=head3 cc
+
+The Country Code
+
+=head3 num
+
+The phone number, including the trunk pointer, area code, and
+subscriber number.
+
+=head3 ext
+
+An extension, if one is present.
+
+=head3 opensrs
+
+The format an OpenSRS Registrar must make a phone number for some
+TLDs.
+
+=head3 human
+
+Human readable format.
+
+=cut
+
+sub orig { $_[0]->{orig} }
+sub cc { $_[0]->{cc} }
+sub num { $_[0]->{num} }
+sub ext { $_[0]->{ext} }
+sub opensrs { $_[0]->{opensrs} }
+sub human { $_[0]->{human} }
+
+1;
+
+__END__
+
+=head1 BUGS
+
+Currently only accept phone numbers in International format. If a
+number isn't given in international format, a false positive could
+occur.
+
+=head1 AUTHOR
+
+Casey West <F<casey@geeknest.com>>
+Maintained by Tim Wilde <F<twilde@dyndns.com>>
+
+=head1 COPYRIGHT
+
+Copyright (c) 2003 Casey West <casey@geeknest.com>.
+Portions Copyright (c) 2005 Dynamic Network Services, Inc.
+
+All rights reserved.
+
+This program is free software; you can redistribute it
+and/or modify it under the same terms as Perl itself.
6 t/bad_numbers.txt
@@ -0,0 +1,6 @@
+1.800.AT.COMPAQ
+(028) 90 41 58 98
+028 777 23456
+028 90 56 47 34
+028 90 458123
+02890712712
91 t/good_numbers.txt
@@ -0,0 +1,91 @@
+600 600 11 22
++ 386 1 5853 449
++48 (22) 64 0001
+36 1 267-4636
++39-02-48230001
+90 212 33 66 999
++61-8-00449552
++33 1 69.87.41.23
+(650) 857-1501
+359 2 731 088
+961 1983 348
+216 1 969 902
+234 1 267 3338
++48 22 5420400
++ 61 2 9870 2131
+971 4 397 5443
++ 44 870 2411963
++421 2 67 296 297
+800820062
+591-3-352-5200
+51-1-211-2828
+808208112
++351 214 122 659
++64 9 356 3093
++212 2 248 47 48
+380 44 230 5102
++27 11 257 0000
+371 760 5004
++39-02-66108000
++263 4 4 94540
+202 331 996
++2542 716 111
+66 2 257-0099
+2542 716 111
++40 1 203 6161
+961 1 983 348
+358 2 141 540 65
+65-6324-8098
+(94 1) 691 571
+370 2 723 035
++386 1 5853 449
++ 64 9 356 3093
+48 22 43 11 999
+7 095 745 5445
++ 27 11 257 0000
+225 20 32 14 16
++65-63951818
+716-871-2919
+420 2 2177 7222
+213 2 37 66 12
++ 2542 716 111
+92 21 111 674 357
+213 2 92 33 66
+54-11-4317-2626
++401-202.78.59
+"233 21 232 041, or 233 21 234 563"
++421 2 5022 2444
++ 852 2904-2198
+33 1 55 174 075
+212 22 40 88 33
++(844)823 0322
++90-216-5797777
+234 1 267 3337
++41 0848 802 208
+966 1 218 0821
+(11) 3328-3396
+968 691 191
+57 1 6069191
++47 23 20 61 00
++ 47 23 162 126
+8148+226-5731
+965 243 1071
++34 901 27 27 27
+372 650 4999
+34 91 377 8170
+44 01229 823793
+070/22.20.90
+01285 654777
+0825.813.823
+01 648 6060
+00 353 1 648 6060
+0120-108908
+092542 716 11
+0800 555 544
+01 365 0776
+0045 38 487 131
+02-567-7881
+00 41 1 800 9201
+00 33 1 55 174 075
+00 39 2 696 334 37
+0800-90-80-90
52 t/numbers.t
@@ -0,0 +1,52 @@
+use Test::More qw[no_plan];
+use strict;
+use FindBin;
+
+use lib qw[lib ../lib];
+
+BEGIN {
+ use_ok 'Parse::PhoneNumber';
+}
+
+my $p = Parse::PhoneNumber->new;
+
+isa_ok $p, 'Parse::PhoneNumber';
+
+open GOOD, "$FindBin::Bin/good_numbers.txt"
+ or die "Can't open numbers: $!";
+while ( <GOOD> ) {
+ chomp;
+ my $number = $p->parse( number => $_ );
+ if ( $number ) {
+ isa_ok $number, 'Parse::PhoneNumber::Number';
+ like $number->cc, qr/^\d{1,3}$/, "Proper CC code: ".$number->cc;
+ is $number->orig, $_, "Original number: ".$number->orig;
+ like $number->num, qr/^\d+$/, "Original matches: $_";
+ like $number->opensrs, qr/\+\d+\.\d+(?:x\d+)?/, "Opensrs syntax correct: ".$number->opensrs;
+ like $number->human, qr/\+\d+\s+\d+(?:x\d+)?/, "Human readable format: ".$number->human;
+ if ( $number->ext ) {
+ like $number->ext, qr/\d+/, "Extension: ".$number->ext;
+ } else {
+ is $number->ext, undef, "No extension is undef";
+ }
+ } else {
+ fail("$_ is good");
+ diag("$_ did not parse, should");
+ }
+}
+close GOOD;
+
+open BAD, "$FindBin::Bin/bad_numbers.txt"
+ or die "Can't open numbers: $!";
+while ( <BAD> ) {
+ chomp;
+ my $number = $p->parse( number => $_ );
+ if ( $number ) {
+ fail("$_ is bad");
+ diag("$_ parsed, should not");
+ } else {
+ ok $p->errstr, "Error string set.";
+ is $number, undef, $p->errstr;
+ }
+}
+close GOOD;
26 t/pod.t
@@ -0,0 +1,26 @@
+# $Id: pod.t,v 1.1 2003/09/29 21:36:31 ctriv Exp $
+
+use Test::More;
+use File::Spec;
+use File::Find;
+use strict;
+
+eval "use Test::Pod 0.95";
+
+if ($@) {
+ plan skip_all => "Test::Pod v0.95 required for testing POD";
+} else {
+ Test::Pod->import;
+
+ my @files;
+ my $blib = File::Spec->catfile(qw(blib lib));
+
+ find( sub { push(@files, $File::Find::name) if /\.p(l|m|od)$/}, $blib);
+
+ plan tests => scalar @files;
+
+ foreach my $file (@files) {
+ pod_file_ok($file);
+ }
+}
+
70 t/us_numbers.t
@@ -0,0 +1,70 @@
+use Test::More qw[no_plan];
+use strict;
+use FindBin;
+
+use lib qw[lib ../lib];
+
+BEGIN {
+ use_ok 'Parse::PhoneNumber';
+}
+
+my $p = Parse::PhoneNumber->new;
+
+isa_ok $p, 'Parse::PhoneNumber';
+
+my @good = qw(
+ 810-555-9841
+ 1-517-842-0924
+ 8927908123
+ 12480982374
+ +1.342.4234.1501
+ 1+823.342.1525
+);
+
+my @bad = qw(
+ 1
+ 1-------------
+ 324-2399
+ 234.2381
+ 23
+ 14876
+ 82-231.4198
+);
+
+
+
+for (@good) {
+ my $number = $p->parse( number => $_, assume_us => 1 );
+ if ( $number ) {
+ isa_ok $number, 'Parse::PhoneNumber::Number';
+ is $number->cc, 1, "Proper CC code: ".$number->cc;
+ is $number->orig, $_, "Original number: ".$number->orig;
+ like $number->num, qr/^\d+$/, "Original matches: $_";
+ like $number->opensrs, qr/\+\d+\.\d+(?:x\d+)?/, "Opensrs syntax correct: ".$number->opensrs;
+ like $number->human, qr/\+\d+\s+\d+(?:x\d+)?/, "Human readable format: ".$number->human;
+ if ( $number->ext ) {
+ like $number->ext, qr/\d+/, "Extension: ".$number->ext;
+ } else {
+ is $number->ext, undef, "No extension is undef";
+ }
+ } else {
+ fail("$_ is good");
+ diag("$_ did not parse, should");
+ }
+}
+
+for (@bad) {
+ my $number = $p->parse( number => $_, assume_us => 1 );
+
+ if ($number) {
+ fail("$_ is bad");
+ diag("$_ parsed, should not");
+ } else {
+ ok("$_ is bad");
+ }
+}
+
+
+__DATA__
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.