line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::IPFromZip; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
16230
|
use 5.018002; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
27
|
|
4
|
1
|
|
|
1
|
|
3
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
29
|
|
5
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
654
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
require Exporter; |
8
|
|
|
|
|
|
|
require Text::CSV_XS; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'all' => [ qw( |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
) ] ); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our @EXPORT = qw( |
19
|
|
|
|
|
|
|
reverse |
20
|
|
|
|
|
|
|
); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# Preloaded methods go here. |
26
|
|
|
|
|
|
|
sub reverse { |
27
|
0
|
|
|
0
|
|
|
my $csvFile; |
28
|
|
|
|
|
|
|
my $postalCode; |
29
|
|
|
|
|
|
|
|
30
|
0
|
|
|
|
|
|
my @output; |
31
|
|
|
|
|
|
|
|
32
|
0
|
0
|
|
|
|
|
if ($_[0] =~ /^[\d-]+$/) { #if only zip code |
33
|
0
|
|
|
|
|
|
$postalCode = $_[0]; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
#looking for csv file if only zip code |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
#seeing if passed in args |
38
|
0
|
0
|
0
|
|
|
|
if (defined($_[1]) && $_[1] =~ /csv/ig) { #if $_[1] is a csv file, or has some mention of anything csv, we should at least try to open it |
39
|
|
|
|
|
|
|
|
40
|
0
|
|
|
|
|
|
$csvFile = $_[1]; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
#checking current dir and /usr/local/share/GeoIP |
44
|
|
|
|
|
|
|
else { |
45
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
|
chomp(my $pwd =`pwd`); |
47
|
0
|
0
|
|
|
|
|
opendir (my $cwd, $pwd) or die "Couldn't open $pwd : $! line 46 \n"; |
48
|
0
|
|
|
|
|
|
while (readdir $cwd) { |
49
|
0
|
0
|
0
|
|
|
|
if (/Blocks/ig && /csv/ig) { |
50
|
0
|
|
|
|
|
|
$csvFile = $_; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
} |
53
|
0
|
|
|
|
|
|
closedir ($cwd); |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
|
56
|
0
|
0
|
|
|
|
|
if (-d "/usr/local/share/GeoIP") { |
57
|
0
|
|
|
|
|
|
opendir(my $geoipDir, "/usr/local/share/GeoIP"); #if directory doesn't exist, then next |
58
|
0
|
0
|
|
|
|
|
if ($geoipDir) { |
59
|
0
|
|
|
|
|
|
while (readdir $geoipDir) { |
60
|
0
|
0
|
0
|
|
|
|
if (/Blocks/ig && /csv/ig) { |
61
|
0
|
|
|
|
|
|
$csvFile = $_; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
0
|
|
|
|
|
|
closedir ($geoipDir); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
0
|
0
|
|
|
|
|
if (!(defined($csvFile))) { |
69
|
0
|
|
|
|
|
|
die ("No CSV found in /usr/local/share/GeoIP or current dir\n"); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
else { #then argv[1] |
77
|
0
|
|
|
|
|
|
$csvFile = $_[0]; #THIS SHOULD BE THE BLOCK FILE |
78
|
0
|
|
|
|
|
|
$postalCode = $_[1]; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
#straight from meta cpan |
84
|
|
|
|
|
|
|
|
85
|
0
|
0
|
|
|
|
|
my $csv = Text::CSV_XS->new ( { binary => 1 } ) # should set binary attribute. |
86
|
|
|
|
|
|
|
or die "Cannot use CSV: ".Text::CSV_XS->error_diag (); |
87
|
|
|
|
|
|
|
|
88
|
0
|
0
|
|
|
|
|
open my $fh, "<$csvFile" or die $!; |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
#printing ip array if matching postalCode |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
|
93
|
0
|
|
|
|
|
|
my $resultCount = 0; |
94
|
0
|
|
|
|
|
|
while ( my $row = $csv->getline( $fh ) ) { |
95
|
0
|
0
|
|
|
|
|
$row->[6] =~ /$postalCode/ or next; # 3rd field should match |
96
|
0
|
|
|
|
|
|
push ( @output, $row->[0] ); |
97
|
0
|
|
|
|
|
|
$resultCount++; |
98
|
|
|
|
|
|
|
} |
99
|
0
|
0
|
|
|
|
|
$csv->eof or $csv->error_diag(); |
100
|
0
|
|
|
|
|
|
close $fh; |
101
|
0
|
|
|
|
|
|
return \@output; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
1; |
105
|
|
|
|
|
|
|
__END__ |