| 1 | package MaxMind::DB::Writer::FromTextFile; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use 5.008008; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use Encode (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use MaxMind::DB::Writer::Tree; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use Net::Works::Network; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 2 | require Exporter; | |||||||||
| 11 | 2 | our @ISA = qw(Exporter); | |||||||||
| 12 | 2 | our @EXPORT_OK = qw(mmdb_create); | |||||||||
| 13 | 2 | our $VERSION = '0.02'; | |||||||||
| 14 | |||||||||||
| 15 | 1 | ▶ | sub mmdb_create { | ||||||||
|
Subroutine
mmdb_create: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 16 | 1 | my $input_filename = shift; | |||||||||
| 17 | 1 | my $mmdb_filename = shift; | |||||||||
| 18 | |||||||||||
| 19 | 1 | ▶ | return 0 unless -s $input_filename; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | |||||||||||
| 21 | my $tree = MaxMind::DB::Writer::Tree->new( | ||||||||||
| 22 | ip_version => 4, | ||||||||||
| 23 | record_size => 24, | ||||||||||
| 24 | database_type => 'MMDB', | ||||||||||
| 25 | description => { | ||||||||||
| 26 | en => 'MaxMindDB', | ||||||||||
| 27 | }, | ||||||||||
| 28 | 20 | ▶ | map_key_type_callback => sub { 'utf8_string' }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 1 | ); | |||||||||
| 30 | 1 | open my $rfh, "<", $input_filename; | |||||||||
| 31 | 1 | ▶ | while (<$rfh>) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | 10 | chomp; | |||||||||
| 33 | 10 | my ( $iprange, $addr ) = split /\s/, $_; | |||||||||
| 34 | 10 | $addr =~ s/\"//g; | |||||||||
| 35 | 10 | ▶ | $iprange = $iprange . "/32" unless $iprange =~ /\//; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 10 | my $subnet = Net::Works::Network->new_from_string( | |||||||||
| 37 | string => $iprange, | ||||||||||
| 38 | version => 4, | ||||||||||
| 39 | ); | ||||||||||
| 40 | |||||||||||
| 41 | 10 | $tree->insert_network( | |||||||||
| 42 | $subnet, | ||||||||||
| 43 | { | ||||||||||
| 44 | subnet => $subnet->as_string(), | ||||||||||
| 45 | string => $addr, | ||||||||||
| 46 | }, | ||||||||||
| 47 | ); | ||||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 1 | open my $fh, '>', $mmdb_filename; | |||||||||
| 51 | 1 | $tree->write_tree($fh); | |||||||||
| 52 | 1 | return -s $mmdb_filename; | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | 2 | 1; | |||||||||
| 56 | __END__ | ||||||||||