Permalink
Browse files

Initial commit of Parse::PhoneNumber 1.6 from CPAN sources

  • Loading branch information...
0 parents commit 622fc8be1e4e70457e20c1bcf8f445ac81ff5ead @twilde committed Sep 12, 2011
Showing with 636 additions and 0 deletions.
  1. +14 −0 Build.PL
  2. +6 −0 INSTALL
  3. +13 −0 MANIFEST
  4. +20 −0 META.yml
  5. +31 −0 Makefile.PL
  6. +68 −0 README
  7. +239 −0 lib/Parse/PhoneNumber.pm
  8. +6 −0 t/bad_numbers.txt
  9. +91 −0 t/good_numbers.txt
  10. +52 −0 t/numbers.t
  11. +26 −0 t/pod.t
  12. +70 −0 t/us_numbers.t
@@ -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;
@@ -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
@@ -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
@@ -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
@@ -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.
+
@@ -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.
Oops, something went wrong.

0 comments on commit 622fc8b

Please sign in to comment.