File Coverage

blib/lib/MaxMind/DB/Reader.pm
Criterion Covered Total %
statement 19 19 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 27 27 100.0


line stmt bran cond sub pod time code
1             package MaxMind::DB::Reader;
2              
3 21     21   8056 use strict;
  21         25  
  21         506  
4 21     21   67 use warnings;
  21         22  
  21         645  
5              
6             our $VERSION = '1.000013';
7              
8 21     21   434 use 5.010000;
  21         98  
9              
10 21     21   70 use Module::Implementation;
  21         20  
  21         430  
11 21     21   58 use Moo 1.003000 ();
  21         243  
  21         386  
12 21     21   71 use Role::Tiny 1.003002 ();
  21         219  
  21         1477  
13              
14             my $Implementation;
15              
16             {
17             ## no critic (Subroutines::ProhibitCallsToUnexportedSubs)
18             my $loader = Module::Implementation::build_loader_sub(
19             implementations => [ 'XS', 'PP' ],
20             );
21              
22             $Implementation = $loader->();
23             }
24              
25             sub new {
26 22     22 1 16480 shift;
27 22         634 return $Implementation->new(@_);
28             }
29              
30             1;
31              
32             # ABSTRACT: Read MaxMind DB files and look up IP addresses
33              
34             __END__
35              
36             =pod
37              
38             =encoding UTF-8
39              
40             =head1 NAME
41              
42             MaxMind::DB::Reader - Read MaxMind DB files and look up IP addresses
43              
44             =head1 VERSION
45              
46             version 1.000013
47              
48             =head1 SYNOPSIS
49              
50             my $reader = MaxMind::DB::Reader->new( file => 'path/to/database.mmdb' );
51              
52             my $record = $reader->record_for_address('1.2.3.4');
53              
54             =head1 DESCRIPTION
55              
56             This module provides a low-level interface to the L<MaxMind DB file
57             format|http://maxmind.github.io/MaxMind-DB/>.
58              
59             If you are looking for an interface to MaxMind's L<GeoIP2 or GeoLite2
60             downloadable databases|http://dev.maxmind.com/geoip/>, you should also check
61             out the L<GeoIP2> distribution. That distribution provides a higher level OO
62             interface to those databases.
63              
64             This API will work with any MaxMind DB databases, regardless of whether it is
65             a GeoIP2 database or not. In addition, if speed is critical, this API will
66             always be faster than the L<GeoIP2> modules, since it returns results as a raw
67             Perl data structure rather than as an object.
68              
69             =head1 PURE PERL VERSUS XS
70              
71             The MaxMind-DB-Reader distribution ships with a single pure Perl
72             implementation of the Reader API. There is a separate distribution on CPAN,
73             L<MaxMind::DB::Reader::XS>, that provides an XS implementation which links
74             against L<libmaxminddb|http://maxmind.github.io/libmaxminddb/>.
75              
76             The XS implementation is approximately 100 times faster than the pure Perl
77             implementation, so if speed is important to you, we highly recommend that you
78             install it!
79              
80             If you install the XS implementation it will be automatically loaded. You do
81             not need to change your code to take advantage of it.
82              
83             =head1 API
84              
85             This module provides the following API:
86              
87             =head2 MaxMind::DB::Reader->new( file => $path )
88              
89             This method returns a new reader object. Note that the class of the object
90             returned will actually be either L<MaxMind::DB::Reader::PP> or
91             L<MaxMind::DB::Reader::XS>.
92              
93             If you need to check that an object is a valid reader, you should check that
94             the object does the C<MaxMind::DB::Reader::Role::Reader> role.
95              
96             The "file" parameter is a required attribute for the constructor. It must be a
97             string containing a path to a file. The constructor will die if the file
98             provided is not readable.
99              
100             You can also pass an additional parameter, "data_source", which must be a valid
101             filehandle. This is useful in testing. For example, you can have the reader
102             read from a filehandle opened to a scalar reference. Under normal usage, the
103             reader simply opens the provided file to read from.
104              
105             =head2 $reader->record_for_address($ip_address)
106              
107             This method takes an IPv4 or IPv6 address as a string. This can be either a
108             dotted quad (C<1.2.3.4>) or any valid IPv6 format (C<abcd::1234>,
109             C<::1.2.3.4>, etc.).
110              
111             This method will die if the address is not a valid IP address.
112              
113             The method returns the data associated with the IP address. Depending on the
114             contents of the database, this can be a scalar or a reference to an array or
115             hash.
116              
117             =head2 $reader->iterate_search_tree( $data_callback, $node_callback )
118              
119             This method iterates over the entire search tree, calling the callbacks you
120             provided for each data record and node in the tree.
121              
122             Both callbacks are optional (although calling this with neither will do a lot
123             of work for no good reason).
124              
125             The node callback is called for every node in the database's search tree. This
126             callback is called with three arguments. These are the node's number (which is
127             based on its position in the file) and the values of its left and right
128             records. These values are themselves numbers. See the L<MaxMind DB
129             spec|http://maxmind.github.io/MaxMind-DB/> for more details on what node
130             record values mean.
131              
132             The data callback is called for records that point to the database's data
133             section. The first two arguments identify the network that the data record
134             applies to. The first argument is an IP address as an integer and the second
135             is a network mask length. The final argument is the data associated with the
136             network.
137              
138             =head2 $reader->metadata()
139              
140             This method returns a L<MaxMind::DB::Metadata> object for the database.
141              
142             =head2 $reader->file()
143              
144             This method returns the file path passed to the constructor.
145              
146             =head1 VERSIONING POLICY
147              
148             This module uses semantic versioning as described by
149             L<http://semver.org/>. Version numbers can be read as X.YYYZZZ, where X is the
150             major number, YYY is the minor number, and ZZZ is the patch number.
151              
152             =head1 SUPPORT
153              
154             Please report all issues with this code using the GitHub issue tracker at
155             L<https://github.com/maxmind/MaxMind-DB-Reader-perl/issues>.
156              
157             Bugs may be submitted through L<https://github.com/maxmind/MaxMind-DB-Reader-perl/issues>.
158              
159             =head1 AUTHORS
160              
161             =over 4
162              
163             =item *
164              
165             Dave Rolsky <drolsky@maxmind.com>
166              
167             =item *
168              
169             Olaf Alders <oalders@maxmind.com>
170              
171             =back
172              
173             =head1 CONTRIBUTORS
174              
175             =for stopwords Greg Oschwald Mateu X Hunter Ran Eilam William Stevenson
176              
177             =over 4
178              
179             =item *
180              
181             Greg Oschwald <goschwald@maxmind.com>
182              
183             =item *
184              
185             Mateu X Hunter <mhunter@maxmind.com>
186              
187             =item *
188              
189             Ran Eilam <reilam@maxmind.com>
190              
191             =item *
192              
193             William Stevenson <skyblue@skybluecircles.com>
194              
195             =back
196              
197             =head1 COPYRIGHT AND LICENSE
198              
199             This software is Copyright (c) 2017 by MaxMind, Inc.
200              
201             This is free software, licensed under:
202              
203             The Artistic License 2.0 (GPL Compatible)
204              
205             =cut