File Coverage

blib/lib/Port/Generator.pm
Criterion Covered Total %
statement 18 19 94.7
branch 2 2 100.0
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 26 27 96.3


line stmt bran cond sub pod time code
1             package Port::Generator ;
2 2     2   986 use strict;
  2         2  
  2         48  
3 2     2   6 use warnings;
  2         2  
  2         76  
4              
5             our $VERSION = '0.1.2';
6              
7 2     2   1254 use IO::Socket::INET;
  2         27644  
  2         10  
8             use Class::Tiny {
9 2         12 min => 49152,
10             max => 65535,
11             proto => 'tcp',
12             addr => 'localhost',
13 2     2   1681 };
  2         4432  
14              
15             =head1 NAME
16              
17             Port::Generator - pick some unused port
18              
19             =head1 SYNOPSIS
20              
21             my $port_gen = Port::Generator->new();
22             $port_gen->port();
23              
24             =head1 DESCRIPTION
25              
26             =head1 METHODS
27              
28             =head2 new(%attributes)
29              
30             =head3 %attributes
31              
32             =head4 min
33              
34             minimal range of ports
35              
36             default I<49152>
37              
38             =head4 max
39              
40             maximal range of ports
41              
42             default I<65535>
43              
44             =head4 proto
45              
46             socket protocol
47              
48             default I
49              
50             =head4 addr
51              
52             local address
53              
54             default I
55              
56             =head2 port()
57              
58             try find some unused port from C-C ports range
59              
60             each port is check for avialable
61              
62             =cut
63             sub port {
64 7     7 1 6320 my ($self) = @_;
65              
66 7         153 foreach my $port ($self->min .. $self->max) {
67 10         268 my $sock = IO::Socket::INET->new(
68             LocalAddr => $self->addr,
69             LocalPort => $port,
70             Proto => $self->proto,
71             );
72              
73 10 100       2306 if ($sock) {
74 7         50 close $sock;
75              
76 7         28 return $port;
77             }
78             }
79              
80 0           return;
81             }
82              
83             =head1 LICENSE
84              
85             Copyright (C) Avast Software.
86              
87             This library is free software; you can redistribute it and/or modify
88             it under the same terms as Perl itself.
89              
90             =head1 AUTHOR
91              
92             Jan Seidl Eseidl@avast.comE
93              
94             =cut
95              
96             1;