File Coverage

blib/lib/Net/IP/Match/Trie.pm
Criterion Covered Total %
statement 15 17 88.2
branch 3 6 50.0
condition 1 3 33.3
subroutine 4 4 100.0
pod n/a
total 23 30 76.6


line stmt bran cond sub pod time code
1             package Net::IP::Match::Trie;
2              
3 2     2   22646 use strict;
  2         4  
  2         74  
4 2     2   10 use warnings;
  2         4  
  2         56  
5 2     2   50 use 5.008_005;
  2         10  
  2         371  
6              
7             our $VERSION = '1.00';
8              
9             sub import {
10 2     2   26     my $class = shift;
11              
12 2 50       33     $ENV{NIMT_PP} = grep {$_ eq "PP"} @_ unless $ENV{NIMT_PP};
  0         0  
13              
14 2 50       10     unless ($ENV{NIMT_PP}) {
15 2         4         eval { require Net::IP::Match::Trie::XS; };
  2         1102  
16                 }
17 2 50 33     32     if ($@ || $ENV{NIMT_PP}) {
18 0                   require Net::IP::Match::Trie::PP;
19                 }
20             }
21              
22             1;
23              
24             __END__
25            
26             =encoding utf-8
27            
28             =begin html
29            
30             <a href="https://travis-ci.org/hirose31/Net-IP-Match-Trie"><img src="https://travis-ci.org/hirose31/Net-IP-Match-Trie.png?branch=master" alt="Build Status" /></a>
31             <a href="https://coveralls.io/r/hirose31/Net-IP-Match-Trie?branch=master"><img src="https://coveralls.io/repos/hirose31/Net-IP-Match-Trie/badge.png?branch=master" alt="Coverage Status" /></a>
32            
33             =end html
34            
35             =head1 NAME
36            
37             Net::IP::Match::Trie - Efficiently match IP addresses against IP ranges with Trie (prefix tree)
38            
39             =head1 SYNOPSIS
40            
41             use Net::IP::Match::Trie;
42             my $matcher = Net::IP::Match::Trie->new;
43             $matcher->add(google => [qw(66.249.64.0/19 74.125.0.0/16)]);
44             $matcher->add(yahoo => [qw(69.147.64.0/18 209.191.64.0/18 209.131.32.0/19)]);
45             $matcher->add(ask => [qw(66.235.112.0/20)]);
46             $matcher->add(docomo => [qw(124.146.174.0/24 ...)]);
47             $matcher->add(au => [qw(59.135.38.128/25 ...)]);
48             $matcher->add(softbank => [qw(123.108.236.0/24 ...)]);
49             $matcher->add(willcom => [qw(61.198.128.0/24 ...)]);
50             say $matcher->match_ip("66.249.64.1"); # => "google"
51             say $matcher->match_ip("69.147.64.1"); # => "yahoo"
52             say $matcher->match_ip("192.0.2.1"); # => ""
53            
54             =head1 DESCRIPTION
55            
56             Net::IP::Match::Trie is XS or Pure Perl implementation of matching IP address against Net ranges.
57            
58             Net::IP::Match::Trie uses Trie (prefix tree) data structure, so very fast lookup time (match_ip) but slow setup (add) time.
59             This module is useful for once initialization and a bunch of lookups model such as long life server process.
60            
61             =head1 METHODS
62            
63             =over 4
64            
65             =item B<add>(LABEL => CIDRS)
66            
67             LABEL: Str
68             CIDRS: ArrayRef
69            
70             register CIDRs to internal data tree labeled as "LABEL".
71            
72             =item B<match_ip>(IP)
73            
74             IP: Str
75            
76             return "LABEL" if IP matched registered CIDRs. otherwise return "".
77            
78             =back
79            
80             =head1 AUTHOR
81            
82             HIROSE Masaaki E<lt>hirose31@gmail.comE<gt>
83            
84             =head1 REPOSITORY
85            
86             L<https://github.com/hirose31/p5-net-ip-match-trie>
87            
88             git clone git://github.com/hirose31/p5-net-ip-match-trie.git
89            
90             patches and collaborators are welcome.
91            
92             =head1 SEE ALSO
93            
94             =head1 COPYRIGHT
95            
96             Copyright HIROSE Masaaki
97            
98             =head1 LICENSE
99            
100             This library is free software; you can redistribute it and/or modify
101             it under the same terms as Perl itself.
102            
103             =cut
104            
105