Sort.pm

filter: Statementstmt 97.6% 41 / 42 Branchbran 81.2% 13 / 16 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 5 / 5 Podpod 100% 2 / 2 totaltotal 94.2% 66 / 70 CC 10 SCAR 15.9
CC
10
Cov
93%
new
7.1
sorter
4.0
BEGIN
1.0
CRAP 4.9
« Sort/port_la.pm ↑ Summary Sort/host_fl.pm »
1 package Net::Connection::Sort;
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  
7 =head1 NAME
8  
9 Net::Connection::Sort - Sorts array of Net::Connection objects.
10  
11 =head1 VERSION
12  
13 Version 0.2.0
14  
15 =cut
16  
17 2 our $VERSION = '0.2.0';
18  
19  
20 =head1 SYNOPSIS
21  
22 use Net::Connection::Sort;
23 use Net::Connection;
24 use Data::Dumper;
25
26 my @objects=(
27 Net::Connection->new({
28 'foreign_host' => '3.3.3.3',
29 'local_host' => '4.4.4.4',
30 'foreign_port' => '22',
31 'local_port' => '11132',
32 'sendq' => '1',
33 'recvq' => '0',
34 'state' => 'ESTABLISHED',
35 'proto' => 'tcp4'
36 }),
37 Net::Connection->new({
38 'foreign_host' => '1.1.1.1',
39 'local_host' => '2.2.2.2',
40 'foreign_port' => '22',
41 'local_port' => '11132',
42 'sendq' => '1',
43 'recvq' => '0',
44 'state' => 'ESTABLISHED',
45 'proto' => 'tcp4'
46 }),
47 Net::Connection->new({
48 'foreign_host' => '5.5.5.5',
49 'local_host' => '6.6.6.6',
50 'foreign_port' => '22',
51 'local_port' => '11132',
52 'sendq' => '1',
53 'recvq' => '0',
54 'state' => 'ESTABLISHED',
55 'proto' => 'tcp4'
56 }),
57 Net::Connection->new({
58 'foreign_host' => '3.3.3.3',
59 'local_host' => '4.4.4.4',
60 'foreign_port' => '22',
61 'local_port' => '11132',
62 'sendq' => '1',
63 'recvq' => '0',
64 'state' => 'ESTABLISHED',
65 'proto' => 'tcp4'
66 }),
67 );
68
69 my $sort_args={
70 type=>'host_f',
71 invert=>0,
72 };
73
74 my $mcs;
75 eval{
76 $mcs=Net::Connection::Sort->new( $sort_args );
77 };
78
79 if ( ! defined( $mcs ) ){
80 print "Failed to init the sorter... ".$@;
81 }
82
83 my @sorted=$mcs->sorter( \@objects );
84
85 print Dumper( \@sorted );
86  
87 =head1 METHODS
88  
89 =head2 new
90  
91 This initiates the module.
92  
93 One argument is taken and that is a hash ref with two possible keys,
94 'type' and 'invert'. If not passed or any of the keys are undef, then
95 the defaults will be used.
96  
97 'type' is the module to use. It is relative to 'Net::Connection::Sort',
98 so 'host_f' becomes 'Net::Connection::Sort::host_f'. Only word characters
99 are accepted here and anything else will result in it dying. See
100 L</SORT TYPES> below for the list of what may be used.
101  
102 'invert' reverses the order returned by the sorter if set to true.
103  
104 my $sort_args={
105 type=>'host_f',
106 invert=>0,
107 };
108
109 my $mcs;
110 eval{
111 $mcs=Net::Connection::Sort->new( $sort_args );
112 };
113
114 if ( ! defined( $mcs ) ){
115 print "Failed to init the sorter... ".$@;
116 }
117  
118 =cut
119  
120 4 ▶sub new{
Subroutine new: called CC=7 SCAR=19.6
Pod documented
121 4 my %args;
122 4 ▶ if(defined($_[1])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 4 0 4
123 4 %args= %{$_[1]};
124 };
125  
126  
127 4 my $self = {
128 testing=>0,
129 type=>'host_f',
130 invert=>0,
131 sorter=>undef,,
132 };
133 4 bless $self;
134  
135 # real in the args if needed
136 4 ▶ if (defined( $args{type} )){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"type"}) 4 0 4
137 4 $self->{type}=$args{type};
138 }
139 4 ▶ if (defined( $args{invert} )){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"invert"}) 1 3 4
140 1 $self->{invert}=$args{invert};
141 }
142  
143 # The type is used to build a module name and path below, so make sure it
144 # is nothing more than a bare module name. Anything else is a injection
145 # attempt and the type is likely to have come from something such as a
146 # command line switch. The capture is also used to untaint it.
147 4 ▶ if ( $self->{type} =~ /^([A-Za-z0-9_]+)$/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'type'} =~ /^([A-Za-z0-9_]+)$/) { } 3 1 4
148 3 $self->{type}=$1;
149 }else{
150 1 die( '"'.$self->{type}.'" is not a usable sort type name');
151 }
152  
153 3 my $module='Net::Connection::Sort::'.$self->{type};
154  
155 # see of we amd reel in the module
156 3 eval{
157 3 require 'Net/Connection/Sort/'.$self->{type}.'.pm';
158 2 $self->{sorter}=$module->new;
159 };
160 3 ▶ if ( $@ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 2 3
161 1 die('Failed to use or invoke '.$module.'->new... '.$@);
162 }
163  
164 # make sure we did get it
165 2 ▶ if (!defined( $self->{sorter} )){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"sorter"}) 0 2 2
166 0 die( $module.'->new returned undef');
167 }
168  
169 2 return $self;
170 }
171  
172 =head2 sorter
173  
174 This sorts the array of Net::Connection objects.
175  
176 One object is taken and that is a array of objects. Anything else will
177 result in it dying.
178  
179 my @sorted=$mcs->sorter( \@objects );
180  
181 print Dumper( \@sorted );
182  
183 =cut
184  
185 5 ▶sub sorter{
Subroutine sorter: called CC=4 SCAR=13.9
Pod documented
186 5 my $self=$_[0];
187 5 my @objects;
188 5 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1] and ref $_[1] eq 'ARRAY') { } 2 3 5
Condition100%3/3
defined $_[1] and ref $_[1] eq "ARRAY"
!ll&&!rl&&r
122
MC/DC100%2/2
defined $_[1] and ref $_[1] eq "ARRAY"
defined $_[1] ref $_[1] eq "ARRAY"
Truth table100%3/3
A and B
A= defined $_[1]B= ref $_[1] eq "ARRAY"
ABresult
0X0
100
111
189 defined( $_[1] ) &&
190 ( ref($_[1]) eq 'ARRAY' )
191 ){
192 2 @objects=@{ $_[1] };
193 }else{
194 3 die 'The passed item is either not a array or undefined';
195 }
196  
197 2 my @sorted=$self->{sorter}->sorter( \@objects );
198  
199 2 ▶ if ( $self->{invert} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"invert"}) 1 1 2
200 1 return reverse( @sorted );
201 }
202  
203 1 return @sorted;
204 }
205  
206 =head1 SORT TYPES
207  
208 Any of the below may be used as the 'type' when calling new. Anything else
209 will result in new dying.
210  
211 =over 4
212  
213 =item host_f - Host, foreign
214  
215 =item host_fl - Host, foreign then local
216  
217 =item host_l - Host, local
218  
219 =item host_lf - Host, local then foreign
220  
221 =item pid - Process ID
222  
223 =item port_f - Port, foreign, numeric
224  
225 =item port_fa - Port, foreign, alpha
226  
227 =item port_l - Port, local, numeric
228  
229 =item port_la - Port, local, alpha
230  
231 =item proto - Network connection protocol
232  
233 =item ptr_f - PTR, foreign
234  
235 =item ptr_l - PTR, local
236  
237 =item state - Connection state
238  
239 =item uid - User ID
240  
241 =item unsorted - Leaves the order as is
242  
243 =item user - Username
244  
245 =back
246  
247 The alpha port types sort on the service name where there is one. Ports
248 without a service name sort after those with one, numerically.
249  
250 =head1 AUTHOR
251  
252 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
253  
254 =head1 BUGS
255  
256 Please report any bugs or feature requests to C<bug-net-connection-sort at rt.cpan.org>, or through
257 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Connection-Sort>. I will be notified, and then you'll
258 automatically be notified of progress on your bug as I make changes.
259  
260  
261  
262  
263 =head1 SUPPORT
264  
265 You can find documentation for this module with the perldoc command.
266  
267 perldoc Net::Connection::Sort
268  
269  
270 You can also look for information at:
271  
272 =over 4
273  
274 =item * RT: CPAN's request tracker (report bugs here)
275  
276 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Connection-Sort>
277  
278 =item * Search CPAN
279  
280 L<https://metacpan.org/release/Net-Connection-Sort>
281  
282 =item * Git Repo
283  
284 L<https://gitea.eesdp.org/vvelox/Net-Connection-Sort>
285  
286 =back
287  
288  
289 =head1 ACKNOWLEDGEMENTS
290  
291  
292 =head1 LICENSE AND COPYRIGHT
293  
294 This software is Copyright (c) 2019 by Zane C. Bowers-Hadley.
295  
296 This is free software, licensed under:
297  
298 The Artistic License 2.0 (GPL Compatible)
299  
300  
301 =cut
302  
303 2 1; # End of Net::Connection::Sort
« Sort/port_la.pm ↑ Summary Sort/host_fl.pm »