File Coverage

blib/lib/Data/Sah/Coerce/perl/obj/str_net_ipv4.pm
Criterion Covered Total %
statement 16 16 100.0
branch n/a
condition 1 2 50.0
subroutine 5 5 100.0
pod 0 2 0.0
total 22 25 88.0


line stmt bran cond sub pod time code
1             package Data::Sah::Coerce::perl::obj::str_net_ipv4;
2              
3             our $DATE = '2019-07-25'; # DATE
4             our $VERSION = '0.007'; # VERSION
5              
6 1     1   19124 use 5.010001;
  1         4  
7 1     1   7 use strict;
  1         2  
  1         21  
8 1     1   4 use warnings;
  1         2  
  1         163  
9              
10             sub meta {
11             +{
12 1     1 0 11 v => 4,
13             might_fail => 1,
14             prio => 50,
15             };
16             }
17              
18             sub coerce {
19 1     1 0 14 my %args = @_;
20              
21 1         2 my $dt = $args{data_term};
22              
23 1         2 my $res = {};
24              
25 1   50     7 $res->{modules}{'NetAddr::IP'} //= 0;
26 1         2 $res->{expr_match} = "!ref($dt)";
27 1         4 $res->{expr_coerce} = join(
28             "",
29             "do { my \$ip = NetAddr::IP->new($dt); if (!\$ip) { ['Invalid IP address syntax'] } ",
30             "elsif (\$ip->bits != 32) { ['Not an IPv4 address (probably IPv6)'] } ",
31             "elsif (\$ip->masklen != 32) { ['Not a single IPv4 address (an IP range)'] } ",
32             "else { [undef, \$ip] } ",
33             "}",
34             );
35              
36 1         3 $res;
37             }
38              
39             1;
40             # ABSTRACT: Coerce IPv4 address object from string
41              
42             __END__
43              
44             =pod
45              
46             =encoding UTF-8
47              
48             =head1 NAME
49              
50             Data::Sah::Coerce::perl::obj::str_net_ipv4 - Coerce IPv4 address object from string
51              
52             =head1 VERSION
53              
54             This document describes version 0.007 of Data::Sah::Coerce::perl::obj::str_net_ipv4 (from Perl distribution Sah-Schemas-Net), released on 2019-07-25.
55              
56             =head1 DESCRIPTION
57              
58             =for Pod::Coverage ^(meta|coerce)$
59              
60             =head1 HOMEPAGE
61              
62             Please visit the project's homepage at L<https://metacpan.org/release/Sah-Schemas-Net>.
63              
64             =head1 SOURCE
65              
66             Source repository is at L<https://github.com/perlancar/perl-Sah-Schemas-Net>.
67              
68             =head1 BUGS
69              
70             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Sah-Schemas-Net>
71              
72             When submitting a bug or request, please include a test-file or a
73             patch to an existing test-file that illustrates the bug or desired
74             feature.
75              
76             =head1 AUTHOR
77              
78             perlancar <perlancar@cpan.org>
79              
80             =head1 COPYRIGHT AND LICENSE
81              
82             This software is copyright (c) 2019, 2018, 2016 by perlancar@cpan.org.
83              
84             This is free software; you can redistribute it and/or modify it under
85             the same terms as the Perl 5 programming language system itself.
86              
87             =cut