Skip to content

Instantly share code, notes, and snippets.

@renatocron
Created November 28, 2016 22:25
Show Gist options
  • Save renatocron/cbc6fec9e2bedc37e3a03ad86b961316 to your computer and use it in GitHub Desktop.
Save renatocron/cbc6fec9e2bedc37e3a03ad86b961316 to your computer and use it in GitHub Desktop.
use strict;
my @tests = (qw/+5511951345146 +551138765980 +550CR1138765980 +550CR11951345146 +55CR1138765980/);
for my $telnum (@tests) {
my $info = &parsetelephone($telnum);
use DDP;
p [ $telnum, $info ];
}
sub parsetelephone {
my ($tel) = @_;
my $len = length $tel;
use DDP;
p [ $tel, $len ];
my $country = substr( $tel, 1, 2 );
my $area = undef;
my $number = undef;
my $type = 'unknown';
my $carrier;
if ( $country == 55 ) {
if ( $len == 14 ) {
# celular com 9
$area = substr( $tel, 3, 2 );
$number = substr( $tel, -9 );
$type = 'mobile';
}
elsif ( $len == 13 ) {
# telefones
$area = substr( $tel, 3, 2 );
$number = substr( $tel, -8 );
$type = 'fixed_line';
}
elsif ( $len == 16 ) {
# telefone com operadora com o zero
$carrier = substr( $tel, 4, 2 );
$area = substr( $tel, 6, 2 );
$number = substr( $tel, -8 );
$type = 'fixed_line';
}
elsif ( $len == 17 ) {
# celular com operadora
$carrier = substr( $tel, 4, 2 );
$area = substr( $tel, 6, 2 );
$number = substr( $tel, -9 );
$type = 'mobile';
}
elsif ( $len == 15 ) {
# telefone com operadora
$carrier = substr( $tel, 3, 2 );
$area = substr( $tel, 6, 2 );
$number = substr( $tel, -8 );
$type = 'fixed_line';
}
}
return {
country => $country,
area => $area,
number => $number,
carrier_code => $carrier
};
}
__DATA___
Printing in line 18 of /tmp/foo.pl:
[
[0] "+5511951345146",
[1] 14
]
Printing in line 9 of /tmp/foo.pl:
[
[0] "+5511951345146",
[1] {
area : 11,
carrier_code: undef,
country : 55,
number : 951345146
}
]
Printing in line 18 of /tmp/foo.pl:
[
[0] "+551138765980",
[1] 13
]
Printing in line 9 of /tmp/foo.pl:
[
[0] "+551138765980",
[1] {
area : 11,
carrier_code: undef,
country : 55,
number : 38765980
}
]
Printing in line 18 of /tmp/foo.pl:
[
[0] "+550CR1138765980",
[1] 16
]
Printing in line 9 of /tmp/foo.pl:
[
[0] "+550CR1138765980",
[1] {
area : 11,
carrier_code: "CR",
country : 55,
number : 38765980
}
]
Printing in line 18 of /tmp/foo.pl:
[
[0] "+550CR11951345146",
[1] 17
]
Printing in line 9 of /tmp/foo.pl:
[
[0] "+550CR11951345146",
[1] {
area : 11,
carrier_code: "CR",
country : 55,
number : 951345146
}
]
Printing in line 18 of /tmp/foo.pl:
[
[0] "+55CR1138765980",
[1] 15
]
Printing in line 9 of /tmp/foo.pl:
[
[0] "+55CR1138765980",
[1] {
area : 13,
carrier_code: "CR",
country : 55,
number : 38765980
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment