Net::EmptyPort(3)
π Net::EmptyPort π
okay bestie, so this is a User Contributed Perl Documentation page for the module Net::EmptyPort, and honestly? it's giving "tiny module, huge save." it does exactly one thing and it ate: it finds you a free TCP/UDP port so your tests stop colliding with each other. no cap. π
β¨ NAME β¨
Net::EmptyPort β find a free TCP/UDP port. that's it. that's the whole vibe. π¦
πΈ SYNOPSIS πΈ
use Net::EmptyPort qw(empty_port check_port);
# get a socket listening on a random free port
my $socket = listen_socket();
# get a random free port
my $port = empty_port();
# check if a port is already used
if (check_port(5000)) {
say "Port 5000 already in use";
}
like, look at it. three lines and you're done. main character energy fr. β
π DESCRIPTION π
Net::EmptyPort helps finding an empty TCP/UDP port. that's the whole mission statement, and it delivers. π«Ά
π METHODS π
π listen_socket()
π listen_socket(\%args)
my $socket = listen_socket();
Returns a socket listening on a free port. yes, an actual socket, already listening, already slaying. π
The function recognizes the following keys in the hashref argument.
"host"β the address on which to listen. Default is127.0.0.1."proto"β name of the protocol. Default is"tcp". You can get an UDP socket by specifying"udp".
π¦ empty_port()
π¦ empty_port(\%args)
π¦ empty_port($port)
π¦ "empty_port($port, $proto)"
my $port = empty_port();
Returns a port number that is NOT in use. (emphasis on the NOT, because that's literally the entire point, bestie π)
The function recognizes the following keys when given a hashref as the argument.
"host"β specifies the address on which the search should be performed. Default is127.0.0.1."port"β lower bound of the search for an empty port. If omitted, the function searches for an empty port within49152..65535.
See <http://www.iana.org/assignments/port-numbers>
"proto"β name of the protocol. Default is"tcp". You can find an empty UDP port by specifying"udp".
To maintain backwards compatibility, the function accepts scalar arguments as well. For example, you can also find an empty UDP port by specifying the protocol as the second parameter:
my $port = empty_port(1024, 'udp');
# use 49152..65535 range
my $port = empty_port(undef, 'udp');
okay the undef thing is lowkey iconic β you pass undef as the lower bound and it just defaults back to 49152..65535. king behavior. π
π§ check_port(\%args)
π§ check_port($port)
π§ "check_port($port, $proto)"
my $true_or_false = check_port(5000);
Checks if the given port is already in use. Returns true if it is in use (i.e. if the port is NOT free). Returns false if the port is free. so likeβ¦ the return value is the opposite of what the name suggests, which is a little gender-fuck of a function name but we respect it. π
The function recognizes the following keys when given a hashref as the argument.
β οΈ okay this next bit is the important lore, don't skip it β οΈ When UDP is specified as the protocol, thecheck_portfunction sends a probe UDP packet to the designated port to see if an ICMP error message is returned, which indicates that the port is unassigned. The port is assumed to be assigned, unless such response is observed within0.1seconds.
so yeah. UDP checking is a whole vibe check with a 0.1 second timeout. if it doesn't hear back fast enough, it assumes the port is taken. respect the speed. π₯
"host"β specifies the address on which the search should be performed. Default is127.0.0.1."port"β specifies the port to check. This argument is mandatory. (mandatory, bestie. you cannot skip it. π)"proto"β name of the protocol. Default is"tcp".
To maintain backwards compatibility, the function accepts scalar arguments as well in the form described above.
π wait_port(\%args)
π wait_port($port)
π "wait_port($port, $max_wait)"
π "wait_port($port, $max_wait, $proto)"
Waits until a particular port becomes ready to connect to. Returns true if the port becomes ready, or false if otherwise. it's the "are we there yet" of functions, except it actually stops asking eventually. β¨
The function recognizes the following keys when given a hashref as the argument.
"host"β specifies the address on which the search should be performed. Default is127.0.0.1."port"β specifies the port to check. This argument is mandatory."max_wait"β maximum seconds to wait for (default is10seconds). Pass a negative value to wait infinitely. yes, infinitely. it will simply keep waiting forever, which is either dedication or a hostage situation depending on your CI pipeline. π"proto"β name of the protocol. Default is"tcp".
To maintain backwards compatibility, the function accepts scalar arguments as well in the form described above.
π¨ Incompatible changes: Before2.0,"wait_port($port:Int[, $sleep:Number, $retry:Int, $proto:String])"is a signature. so if you're reading ancient code, the signature used to be completely different. we do not stan the old signature. we moved on. periodt. π
π can_bind($host)
π "can_bind($host, $port)"
π "can_bind($host, $port, $proto)"
Checks if the application is capable of binding to given port. simple. elegant. never asks follow-up questions. π«Ά
β AUTHOR β
Tokuhiro Matsuno <tokuhirom@gmail.com>
π THANKS TO π
- kazuhooku
- dragon3
- charsbar
- Tatsuhiko Miyagawa
- lestrrat
the whole group chat came through for this one. shoutout. π
π SEE ALSO π
ngl there is literally nothing here. this section is empty. a blank canvas. an aesthetic. we're leaving it exactly as we found it. π€·ββοΈ
π LICENSE π
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
open source, free to remix, free to redistribute. we love a permissive queen. π
and that's the whole page, bestie!! this was Net::EmptyPort(3) from perl v5.42.0, dated 2019-10-09 β written by the User Contributed Perl Documentation crew and narrated to you with love. mwah. πβ¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-Test-TCP 2.22-160000.2.2 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms