Sort/host_lf.pm

filter: Statementstmt 89.4% 34 / 38 Branchbran 50.0% 4 / 8 Conditioncond 44.4% 4 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 7 / 7 Podpod 100% 3 / 3 totaltotal 73.2% 52 / 71 CC 8 SCAR 13.7
CC
8
Cov
76%
helper
6.6
sorter
4.3
BEGIN
1.0
CRAP 3.9
« Sort/host_fl.pm ↑ Summary Sort/host_f.pm »
1 package Net::Connection::Sort::host_lf;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Net::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Net::Connection::Sort::host_lf - Sorts the connections via the local host and then foreign host.
11  
12 =head1 VERSION
13  
14 Version 0.1.4
15  
16 =cut
17  
18 2 our $VERSION = '0.1.4';
19  
20  
21 =head1 SYNOPSIS
22  
23 use Net::Connection::Sort::host_lf;
24 use Net::Connection;
25 use Data::Dumper;
26
27 my @objects=(
28 Net::Connection->new({
29 'foreign_host' => '3.3.3.4',
30 'local_host' => '4.4.4.5',
31 'foreign_port' => '22',
32 'local_port' => '11132',
33 'sendq' => '1',
34 'recvq' => '0',
35 'state' => 'ESTABLISHED',
36 'proto' => 'tcp4'
37 }),
38 Net::Connection->new({
39 'foreign_host' => '1.1.1.1',
40 'local_host' => '2.2.2.2',
41 'foreign_port' => '22',
42 'local_port' => '11132',
43 'sendq' => '1',
44 'recvq' => '0',
45 'state' => 'ESTABLISHED',
46 'proto' => 'tcp4'
47 }),
48 Net::Connection->new({
49 'foreign_host' => '5.5.5.5',
50 'local_host' => '6.6.6.6',
51 'foreign_port' => '22',
52 'local_port' => '11132',
53 'sendq' => '1',
54 'recvq' => '0',
55 'state' => 'ESTABLISHED',
56 'proto' => 'tcp4'
57 }),
58 Net::Connection->new({
59 'foreign_host' => '3.3.3.3',
60 'local_host' => '4.4.4.4',
61 'foreign_port' => '22',
62 'local_port' => '11132',
63 'sendq' => '1',
64 'recvq' => '0',
65 'state' => 'ESTABLISHED',
66 'proto' => 'tcp4'
67 }),
68 );
69
70 my $sorter=Net::Connection::Sort::host_lf->new;
71
72 @objects=$sorter->sorter( \@objects );
73
74 print Dumper( \@objects );
75  
76 =head1 METHODS
77  
78 =head2 new
79  
80 This initiates the module.
81  
82 No arguments are taken and this will always succeed.
83  
84 my $sorter=Net::Connection::Sort::host_lf->new;
85  
86 =cut
87  
88 1 ▶sub new{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
89 1 my $self = {
90 };
91 1 bless $self;
92  
93 1 return $self;
94 }
95  
96 =head2 sorter
97  
98 This sorts the array of Net::Connection objects.
99  
100 One object is taken and that is a array of objects.
101  
102 @objects=$sorter->sorter( \@objects );
103
104 print Dumper( \@objects );
105  
106 =cut
107  
108 2 ▶sub sorter{
Subroutine sorter: called CC=4 SCAR=14.6
Pod documented
109 2 my $self=$_[0];
110 2 my @objects;
111 2 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1] and ref $_[1] eq 'ARRAY') { } 2 0 2
Condition33%1/3
defined $_[1] and ref $_[1] eq "ARRAY"
!ll&&!rl&&r
002
MC/DC0%0/2
defined $_[1] and ref $_[1] eq "ARRAY"
defined $_[1] ref $_[1] eq "ARRAY"
Truth table33%1/3
A and B
A= defined $_[1]B= ref $_[1] eq "ARRAY"
ABresult
0X0
100
111
112 defined( $_[1] ) &&
113 ( ref($_[1]) eq 'ARRAY' )
114 ){
115 2 @objects=@{ $_[1] };
116 }else{
117 0 die 'The passed item is either not a array or undefined';
118 }
119  
120 # Net::IP is not cheap, so the values to sort on are worked out once per
121 # connection here instead of once per comparison in the sort block.
122 7 @objects=map { $_->[2] }
123 8 ▶ sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] }
Condition66%2/3
$Net::Connection::Sort::host_lf::a->[0] <=> $Net::Connection::Sort::host_lf::b->[0] || $Net::Connection::Sort::host_lf::a->[1] <=> $Net::Connection::Sort::host_lf::b->[1]
l!l&&r!l&&!r
710
MC/DC0%0/2
$Net::Connection::Sort::host_lf::a->[0] <=> $Net::Connection::Sort::host_lf::b->[0] || $Net::Connection::Sort::host_lf::a->[1] <=> $Net::Connection::Sort::host_lf::b->[1]
$Net::Connection::Sort::host_lf::a->[0] <=> $Net::Connection::Sort::host_lf::b->[0] $Net::Connection::Sort::host_lf::a->[1] <=> $Net::Connection::Sort::host_lf::b->[1]
Truth table66%2/3
A || B
A= $Net::Connection::Sort::host_lf::a->[0] <=> $Net::Connection::Sort::host_lf::b->[0]B= $Net::Connection::Sort::host_lf::a->[1] <=> $Net::Connection::Sort::host_lf::b->[1]
ABresult
1X1
011
000
124 2 map { [ helper( $_->local_host ), helper( $_->foreign_host ), $_ ] } @objects;
125  
126 2 return @objects;
127 }
128  
129 =head2 helper
130  
131 This is a internal function.
132  
133 =cut
134  
135 14 ▶sub helper{
Subroutine helper: called CC=5 SCAR=18.9
Pod documented
136 14 ▶ if ( !defined($_[0]) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[0]) 0 14 14
137 0 return 0;
138 }
139 # Link local addresses arrive with a zone id attached, such as
140 # fe80::1%eth0. Neither the check below nor Net::IP will take one.
141 14 my $address=$_[0];
142 14 $address=~s/\%.*$//;
143 14 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($address eq "*" or $address =~ /[g-zG-Z]/) 0 14 14
Condition33%1/3
$address eq "*" or $address =~ /[g-zG-Z]/
l!l&&r!l&&!r
0014
MC/DC0%0/2
$address eq "*" or $address =~ /[g-zG-Z]/
$address eq "*" $address =~ /[g-zG-Z]/
Truth table33%1/3
A or B
A= $address eq "*"B= $address =~ /[g-zG-Z]/
ABresult
1X1
011
000
144 ( $address eq '*' ) ||
145 ( $address =~ /[g-zG-Z]/ )
146 ){
147 0 return 0;
148 }
149 14 my $host=eval { Net::IP->new( $address )->intip} ;
150 14 ▶ if (!defined( $host )){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $host) 0 14 14
151 0 return 0;
152 }
153 14 return $host;
154 }
155  
156 =head1 AUTHOR
157  
158 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
159  
160 =head1 BUGS
161  
162 Please report any bugs or feature requests to C<bug-net-connection-sort at rt.cpan.org>, or through
163 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Connection-Sort>. I will be notified, and then you'll
164 automatically be notified of progress on your bug as I make changes.
165  
166  
167  
168  
169 =head1 SUPPORT
170  
171 You can find documentation for this module with the perldoc command.
172  
173 perldoc Net::Connection::Sort
174  
175  
176 You can also look for information at:
177  
178 =over 4
179  
180 =item * RT: CPAN's request tracker (report bugs here)
181  
182 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Connection-Sort>
183  
184 =item * Search CPAN
185  
186 L<https://metacpan.org/release/Net-Connection-Sort>
187  
188 =item * Git Repo
189  
190 L<https://gitea.eesdp.org/vvelox/Net-Connection-Sort>
191  
192 =back
193  
194  
195 =head1 ACKNOWLEDGEMENTS
196  
197  
198 =head1 LICENSE AND COPYRIGHT
199  
200 This software is Copyright (c) 2019 by Zane C. Bowers-Hadley.
201  
202 This is free software, licensed under:
203  
204 The Artistic License 2.0 (GPL Compatible)
205  
206  
207 =cut
208  
209 2 1; # End of Net::Connection::Sort
« Sort/host_fl.pm ↑ Summary Sort/host_f.pm »