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   10336 use strict;
  21         60  
  21         743  
4 21     21   124 use warnings;
  21         59  
  21         880  
5              
6             our $VERSION = '1.000014';
7              
8 21     21   515 use 5.010000;
  21         177  
9              
10 21     21   129 use Module::Implementation;
  21         45  
  21         564  
11 21     21   111 use Moo 1.003000 ();
  21         338  
  21         470  
12 21     21   131 use Role::Tiny 1.003002 ();
  21         343  
  21         2100  
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 23110 shift;
27 22         627 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.000014
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             This module is deprecated and will only receive fixes for major bugs and
155             security vulnerabilities. New features and functionality will not be added.
156              
157             Please report all issues with this code using the GitHub issue tracker at
158             L<https://github.com/maxmind/MaxMind-DB-Reader-perl/issues>.
159              
160             Bugs may be submitted through L<https://github.com/maxmind/MaxMind-DB-Reader-perl/issues>.
161              
162             =head1 AUTHORS
163              
164             =over 4
165              
166             =item *
167              
168             Dave Rolsky <drolsky@maxmind.com>
169              
170             =item *
171              
172             Olaf Alders <oalders@maxmind.com>
173              
174             =back
175              
176             =head1 CONTRIBUTORS
177              
178             =for stopwords Greg Oschwald Mark Fowler Mateu X Hunter Ran Eilam William Stevenson Will Storey
179              
180             =over 4
181              
182             =item *
183              
184             Greg Oschwald <goschwald@maxmind.com>
185              
186             =item *
187              
188             Mark Fowler <mark@twoshortplanks.com>
189              
190             =item *
191              
192             Mateu X Hunter <mhunter@maxmind.com>
193              
194             =item *
195              
196             Ran Eilam <reilam@maxmind.com>
197              
198             =item *
199              
200             William Stevenson <skyblue@skybluecircles.com>
201              
202             =item *
203              
204             Will Storey <wstorey@maxmind.com>
205              
206             =back
207              
208             =head1 COPYRIGHT AND LICENSE
209              
210             This software is Copyright (c) 2019 by MaxMind, Inc.
211              
212             This is free software, licensed under:
213              
214             The Artistic License 2.0 (GPL Compatible)
215              
216             =cut