File Coverage

blib/lib/Port/Selector.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::Selector;
2 2     2   1362 use strict;
  2         4  
  2         64  
3 2     2   10 use warnings;
  2         2  
  2         109  
4              
5             our $VERSION = '0.1.6';
6              
7 2     2   1232 use IO::Socket::INET;
  2         48599  
  2         23  
8             use Class::Tiny {
9 2         20 min => 49152,
10             max => 65535,
11             proto => 'tcp',
12             addr => 'localhost',
13 2     2   3094 };
  2         7544  
14              
15             =head1 NAME
16              
17             Port::Selector - pick some unused port
18              
19             =head1 SYNOPSIS
20              
21             my $port_sel = Port::Selector->new();
22             $port_sel->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 10768 my ($self) = @_;
72              
73 7         276 foreach my $port ($self->min .. $self->max) {
74 9         480 my $sock = IO::Socket::INET->new(
75             LocalAddr => $self->addr,
76             LocalPort => $port,
77             Proto => $self->proto,
78             ReuseAddr => $^O ne 'MSWin32',
79             );
80              
81 9 100       3667 if ($sock) {
82 7         74 close $sock;
83              
84 7         50 return $port;
85             }
86             }
87              
88 0           return;
89             }
90              
91             =head1 SEE ALSO
92              
93             L (part of the C distribution,
94             provides a function C
95             which does the same thing as the C method in this module.
96              
97             =head1 LICENSE
98              
99             Copyright (C) Avast Software.
100              
101             This library is free software; you can redistribute it and/or modify
102             it under the same terms as Perl itself.
103              
104             =head1 AUTHOR
105              
106             Jan Seidl Eseidl@avast.comE
107              
108             =cut
109              
110             1;