blib/lib/Template/Plugin/IPAddr.pm

filter: Statementstmt 97.2% 35 / 36 Branchbran 100% 4 / 4 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 94.1% 16 / 17 Podpod 100% 9 / 9 totaltotal 92.9% 66 / 71 CC 4 SCAR 3.4
CC
4
Cov
95%
new
3.0
_compact
2.0
BEGIN
1.0
CRAP 1.4
↑ Summary
1 package Template::Plugin::IPAddr;
2 # ABSTRACT: Template::Toolkit plugin handling IP-addresses
3 1 $Template::Plugin::IPAddr::VERSION = '0.03';
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use base 'Template::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use NetAddr::IP qw{ :lower };
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Scalar::Util qw{ blessed };
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 0 ▶use overload '""' => sub { shift->cidr };
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=0.2
12  
13 9 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
14 9 my ($class, $context, $arg) = @_;
15 # When used as [% USE IPAddr %] or [% USE IPAddr(addr) %]
16 # $context is a Template::Context object, and $arg is filled
17 # with the arguments to IPAddr (undef resp. addr here).
18 # When used as [% ip = NetAddr.new(addr) %], $context contain
19 # the addr.
20 9 ▶ my $addr = blessed $context ? $arg : $context;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($context) ? : 7 2 9
21 9 ▶ return bless { _cidr => NetAddr::IP->new($addr) }, ref $class || $class;
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
270
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
22 }
23  
24 13 ▶sub addr { return _compact(shift->{_cidr}) }
Subroutine addr: called CC=1 SCAR=0.0
Pod documented
25  
26 6 ▶sub addr_cidr {
Subroutine addr_cidr: called CC=1 SCAR=0.0
Pod documented
27 6 my $self = shift;
28 6 return $self->addr . '/' . $self->{_cidr}->masklen;
29 }
30  
31 6 ▶sub cidr {
Subroutine cidr: called CC=1 SCAR=0.0
Pod documented
32 6 my $self = shift;
33  
34 # we can't use the cidr method because we want network/prefix,
35 # and cidr returns addr/prefix.
36 #
37 # return an ipv6 address in compact format (with '::').
38 6 return $self->network . '/' . $self->{_cidr}->masklen;
39 }
40  
41 6 ▶sub first { return _compact(shift->{_cidr}->first) }
Subroutine first: called CC=1 SCAR=0.0
Pod documented
42 6 ▶sub last { return _compact(shift->{_cidr}->last) }
Subroutine last: called CC=1 SCAR=0.0
Pod documented
43 3 ▶sub netmask { return shift->{_cidr}->mask }
Subroutine netmask: called CC=1 SCAR=0.0
Pod documented
44 11 ▶sub network { return _compact(shift->{_cidr}->network) }
Subroutine network: called CC=1 SCAR=0.0
Pod documented
45 3 ▶sub wildcard { return scalar shift->{_cidr}->wildcard }
Subroutine wildcard: called CC=1 SCAR=0.0
Pod documented
46  
47 # This sub takes an NetAddr::IP object and returns
48 # the address in short notation if IPv6, or the
49 # address as is if IPv4.
50 36 ▶sub _compact {
Subroutine _compact: called CC=2 SCAR=6.9
51 36 my $ip = shift;
52 36 ▶ return $ip->addr =~ /:/ ? $ip->short : $ip->addr;
Branch: 1 branch
BranchTrueFalseTotal
$ip->addr =~ /:/ ? : 13 23 36
53 }
54  
55 1 1;
56  
57 __END__
↑ Summary