Net-IP-Match-Regexp
view release on metacpan or search on metacpan
lib/Net/IP/Match/Regexp.pm view on Meta::CPAN
# The tree is a temporary construct. It has three possible
# properties: 0, 1, and code. The code is the return value for a
# match.
my %tree;
IPRANGE:
for ( my $i = 0; $i < @map; $i += 2 ) {
my $range = $map[ $i ];
my $match = $map[ $i + 1 ];
my ( $ip, $mask ) = split m/\//xms, $range;
if (! defined $mask) {
$mask = 32; ## no critic(MagicNumbers)
}
my $tree = \%tree;
my @bits = split m//xms, unpack 'B32', pack 'C4', split m/[.]/xms, $ip;
for my $bit ( @bits[ 0 .. $mask - 1 ] ) {
# If this case is hit, it means that our IP range is a subset
# of some other range, and thus ignorable
next IPRANGE if !$depthfirst && $tree->{code};
$tree->{$bit} ||= {}; # Turn a leaf into a branch, if needed
$tree = $tree->{$bit}; # Follow one branch
}
lib/Net/IP/Match/Regexp.pm view on Meta::CPAN
=cut
sub match_ip {
my ( $ip, $re ) = @_;
return if !$ip;
return if !$re;
local $LAST_REGEXP_CODE_RESULT = undef;
use re 'eval';
( '4' . unpack 'B32', pack 'C4', split m/[.]/xms, $ip ) =~ m/$re/xms;
return $LAST_REGEXP_CODE_RESULT;
}
# Helper function. This recurses to build the regular expression
# string from a tree of IP ranges constructed by
# create_iprange_regexp().
sub _tree2re {
my ( $tree ) = @_;
( run in 0.828 second using v1.01-cache-2.11-cpan-71847e10f99 )