File Coverage

blib/lib/Data/Format/Validate/IP.pm
Criterion Covered Total %
statement 10 10 100.0
branch n/a
condition 2 6 33.3
subroutine 4 4 100.0
pod 0 2 0.0
total 16 22 72.7


line stmt bran cond sub pod time code
1             package Data::Format::Validate::IP;
2             our $VERSION = q/0.3/;
3              
4 2     2   131556 use Carp qw/croak/;
  2         15  
  2         74  
5 2     2   8 use base q/Exporter/;
  2         2  
  2         534  
6              
7             our @EXPORT_OK = qw/
8             looks_like_ipv4
9             looks_like_ipv6
10             /;
11              
12             our %EXPORT_TAGS = (
13             q/all/ => [qw/
14             looks_like_ipv4
15             looks_like_ipv6
16             /]
17             );
18              
19             sub looks_like_ipv4 ($) {
20              
21 5   33 5 0 92 my $ip = shift || croak q/Value must be provided/;
22 5         33 $ip =~ /^
23             (?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3} # Three groups of numbers from 0 to 255 joined by '.'
24             (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) # Final group of numbers, but without '.'
25             $/x
26             }
27              
28             sub looks_like_ipv6 ($) {
29              
30 7   33 7 0 94 my $ip = shift || croak q/Value must be provided/;
31 7         43 $ip =~ /^
32             (?:[A-F0-9]{1,4}:){7} # Seven groups with 1 to 4 hexadecimal digits joined by ':'
33             [A-F0-9]{1,4} # Final group of digits, but without ':'
34             $/ix
35             }
36             1;
37              
38             =pod
39              
40             =encoding utf8
41              
42             =head1 NAME
43              
44             Data::Format::Validate::IP - A IP validating module.
45              
46             =head1 SYNOPSIS
47              
48             Function-oriented module capable of validating the format of IPV4 or IPV6 addresses.
49              
50             =head1 UTILITIES
51              
52             =over 4
53              
54             =item IP (ipv4)
55              
56             use Data::Format::Validate::IP 'looks_like_ipv4';
57              
58             looks_like_ipv4 '127.0.0.1'; # returns 1
59             looks_like_ipv4 '255255255255'; # returns 0
60              
61             =item IP (ipv6)
62              
63             use Data::Format::Validate::IP 'looks_like_ipv6';
64              
65             looks_like_ipv6 '1762:0:0:0:0:B03:1:AF18'; # returns 1
66             looks_like_ipv6 '17620000AFFFB031AF187'; # returns 0
67              
68             =back
69              
70             =head1 CONTRIBUITION
71              
72             This source is on Github:
73              
74             https://github.com/rozcovo/Data-Format-Validate/blob/master/lib/Data/Format/Validate/IP.pm
75              
76             =head1 AUTHOR
77              
78             Created by Israel Batista
79              
80             =cut