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   1038 use strict;
  2         3  
  2         49  
3 2     2   8 use warnings;
  2         2  
  2         78  
4              
5             our $VERSION = '0.1.3';
6              
7 2     2   904 use IO::Socket::INET;
  2         28473  
  2         11  
8             use Class::Tiny {
9 2         12 min => 49152,
10             max => 65535,
11             proto => 'tcp',
12             addr => 'localhost',
13 2     2   1623 };
  2         4255  
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             This module is used to find a free port,
27             by default in the range 49152 to 65535,
28             but you can change the range of ports that will be checked.
29              
30             =head1 METHODS
31              
32             =head2 new(%attributes)
33              
34             =head3 %attributes
35              
36             =head4 min
37              
38             lowest numbered port to consider
39              
40             default I<49152>
41              
42             The range 49152-65535 is commonly used by applications that utilize a
43             dynamic/random/configurable port.
44              
45             =head4 max
46              
47             highest numbered port to consider
48              
49             default I<65535>
50              
51             =head4 proto
52              
53             socket protocol
54              
55             default I
56              
57             =head4 addr
58              
59             local address
60              
61             default I
62              
63             =head2 port()
64              
65             Tries to find an unused port from C-C ports range,
66             checking each port in turn until it finds an available one.
67              
68             =cut
69              
70             sub port {
71 7     7 1 4818 my ($self) = @_;
72              
73 7         140 foreach my $port ($self->min .. $self->max) {
74 10         294 my $sock = IO::Socket::INET->new(
75             LocalAddr => $self->addr,
76             LocalPort => $port,
77             Proto => $self->proto,
78             );
79              
80 10 100       2376 if ($sock) {
81 7         51 close $sock;
82              
83 7         28 return $port;
84             }
85             }
86              
87 0           return;
88             }
89              
90             =head1 SEE ALSO
91              
92             L (part of the C distribution,
93             provides a function C
94             which does the same thing as the C method in this module.
95              
96             =head1 LICENSE
97              
98             Copyright (C) Avast Software.
99              
100             This library is free software; you can redistribute it and/or modify
101             it under the same terms as Perl itself.
102              
103             =head1 AUTHOR
104              
105             Jan Seidl Eseidl@avast.comE
106              
107             =cut
108              
109             1;