Sort/port_fa.pm

filter: Statementstmt 96.4% 27 / 28 Branchbran 83.3% 5 / 6 Conditioncond 50.0% 6 / 12 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 100% 6 / 6 Podpod 100% 3 / 3 totaltotal 78.6% 48 / 61 CC 8 SCAR 16.2
CC
8
Cov
83%
sorter
8.3
helper
2.0
BEGIN
1.0
CRAP 5.1
↑ Summary Sort/port_la.pm »
1 package Net::Connection::Sort::port_fa;
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::port_fa - Sorts the connections via the foreign port alphabetically.
10  
11 =head1 VERSION
12  
13 Version 0.1.2
14  
15 =cut
16  
17 2 our $VERSION = '0.1.2';
18  
19  
20 =head1 SYNOPSIS
21  
22 This sorts on the service name where there is one. Ports without a service
23 name sort after those with one, numerically. For purely numeric sorting use
24 port_f.
25  
26 use Net::Connection::Sort::port_fa;
27 use Net::Connection;
28 use Data::Dumper;
29
30 my @objects=(
31 Net::Connection->new({
32 'foreign_host' => '3.3.3.3',
33 'local_host' => '4.4.4.4',
34 'foreign_port' => 'FTP',
35 'local_port' => '11132',
36 'sendq' => '1',
37 'recvq' => '0',
38 'state' => 'ESTABLISHED',
39 'proto' => 'tcp4',
40 'ports' => 0,
41 }),
42 Net::Connection->new({
43 'foreign_host' => '1.1.1.1',
44 'local_host' => '2.2.2.2',
45 'foreign_port' => 'SSH',
46 'local_port' => '11132',
47 'sendq' => '1',
48 'recvq' => '0',
49 'state' => 'ESTABLISHED',
50 'proto' => 'tcp4',
51 'ports' => 0,
52 }),
53 Net::Connection->new({
54 'foreign_host' => '5.5.5.5',
55 'local_host' => '6.6.6.6',
56 'foreign_port' => 'HTTP',
57 'local_port' => '11132',
58 'sendq' => '1',
59 'recvq' => '0',
60 'state' => 'ESTABLISHED',
61 'proto' => 'tcp4',
62 'ports' => 0,
63 }),
64 Net::Connection->new({
65 'foreign_host' => '3.3.3.3',
66 'local_host' => '4.4.4.4',
67 'foreign_port' => 'HTTPS',
68 'local_port' => '11132',
69 'sendq' => '1',
70 'recvq' => '0',
71 'state' => 'ESTABLISHED',
72 'proto' => 'tcp4',
73 'ports' => 0,
74 }),
75 );
76
77 my $sorter=Net::Connection::Sort::port_fa->new;
78
79 @objects=$sorter->sorter( \@objects );
80
81 print Dumper( \@objects );
82  
83 =head1 METHODS
84  
85 =head2 new
86  
87 This initiates the module.
88  
89 No arguments are taken and this will always succeed.
90  
91 my $sorter=Net::Connection::Sort::port_fa->new;
92  
93 =cut
94  
95 1 ▶sub new{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
96 1 my $self = {
97 };
98 1 bless $self;
99  
100 1 return $self;
101 }
102  
103 =head2 sorter
104  
105 This sorts the array of Net::Connection objects.
106  
107 One object is taken and that is a array of objects.
108  
109 @objects=$sorter->sorter( \@objects );
110
111 print Dumper( \@objects );
112  
113 =cut
114  
115 2 ▶sub sorter{
Subroutine sorter: called CC=7 SCAR=21.1
Pod documented
116 2 my $self=$_[0];
117 2 my @objects;
118 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
119 defined( $_[1] ) &&
120 ( ref($_[1]) eq 'ARRAY' )
121 ){
122 2 @objects=@{ $_[1] };
123 }else{
124 0 die 'The passed item is either not a array or undefined';
125 }
126  
127 @objects=sort {
128 2 my ( $a_nameless, $a_key )=helper( $a );
129 7 my ( $b_nameless, $b_key )=helper( $b );
130  
131 # Named ports sort alphabetically ahead of the nameless ones, which
132 # sort numerically so port 9 does not end up after port 10. Anything
133 # nameless and non-numeric, such as the '*' used for wildcard ports,
134 # falls back to a string comparison.
135 7 ▶ $a_nameless <=> $b_nameless
Branch: 1 branch
BranchTrueFalseTotal
$a_nameless && $a_key =~ /^[0-9]+$/ && $b_key =~ /^[0-9]+$/ ? : 3 4 7
Condition55%5/9
$a_nameless <=> $b_nameless || ($a_nameless && $a_key =~ /^[0-9]+$/ && $b_key =~ /^[0-9]+$/ ? $a_key <=> $b_key : $a_key cmp $b_key)
l!l&&r!l&&!r
070
$a_nameless and $a_key =~ /^[0-9]+$/ and $b_key =~ /^[0-9]+$/
!ll&&!rl&&r
403
$a_nameless and $a_key =~ /^[0-9]+$/
!ll&&!rl&&r
403
MC/DC25%1/4
$a_nameless <=> $b_nameless || ($a_nameless && $a_key =~ /^[0-9]+$/ && $b_key =~ /^[0-9]+$/ ? $a_key <=> $b_key : $a_key cmp $b_key)
$a_nameless <=> $b_nameless $a_nameless $a_key =~ /^[0-9]+$/ $b_key =~ /^[0-9]+$/
Truth table40%2/5
A || B and C and D
A= $a_nameless <=> $b_namelessB= $a_namelessC= $a_key =~ /^[0-9]+$/D= $b_key =~ /^[0-9]+$/
ABCDresult
1XXX1
01111
00XX0
010X0
01100
136 ||
137 (
138 (
139 $a_nameless &&
140 ( $a_key =~ /^[0-9]+$/ ) &&
141 ( $b_key =~ /^[0-9]+$/ )
142 )
143 ? $a_key <=> $b_key
144 : $a_key cmp $b_key
145 )
146 } @objects;
147  
148 2 return @objects;
149 }
150  
151 =head2 helper
152  
153 This is a internal function.
154  
155 Two items are returned for the connection passed to it. The first is true if
156 the port has no service name and the second is the value to sort on, either
157 the service name or the port itself.
158  
159 =cut
160  
161 14 ▶sub helper{
Subroutine helper: called CC=2 SCAR=6.9
Pod documented
162 14 my $port_name=$_[0]->foreign_port_name;
163  
164 14 ▶ if ( !defined( $port_name ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $port_name) 6 8 14
165 6 return ( 1, $_[0]->foreign_port );
166 }
167  
168 8 return ( 0, $port_name );
169 }
170  
171 =head1 AUTHOR
172  
173 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
174  
175 =head1 BUGS
176  
177 Please report any bugs or feature requests to C<bug-net-connection-sort at rt.cpan.org>, or through
178 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Connection-Sort>. I will be notified, and then you'll
179 automatically be notified of progress on your bug as I make changes.
180  
181  
182  
183  
184 =head1 SUPPORT
185  
186 You can find documentation for this module with the perldoc command.
187  
188 perldoc Net::Connection::Sort
189  
190  
191 You can also look for information at:
192  
193 =over 4
194  
195 =item * RT: CPAN's request tracker (report bugs here)
196  
197 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Connection-Sort>
198  
199 =item * Search CPAN
200  
201 L<https://metacpan.org/release/Net-Connection-Sort>
202  
203 =item * Git Repo
204  
205 L<https://gitea.eesdp.org/vvelox/Net-Connection-Sort>
206  
207 =back
208  
209  
210 =head1 ACKNOWLEDGEMENTS
211  
212  
213 =head1 LICENSE AND COPYRIGHT
214  
215 This software is Copyright (c) 2019 by Zane C. Bowers-Hadley.
216  
217 This is free software, licensed under:
218  
219 The Artistic License 2.0 (GPL Compatible)
220  
221  
222 =cut
223  
224 2 1; # End of Net::Connection::Sort
↑ Summary Sort/port_la.pm »