Socket...:Strict(3pm)
π Socket::GetAddrInfo::Strict β bestie edition π
okay so. you know Socket::GetAddrInfo? the perl module? this is its β¨strictβ¨ sibling, and it lives in your perl docs as Socket::GetAddrInfo::Strict(3pm). and literally the whole personality of this module is: Provide Socket::GetAddrInfo functions which throw exceptions. that's it. that's the girl. π
β¨ SYNOPSIS β¨
use Socket qw( SOCK_STREAM );
use Socket::GetAddrInfo::Strict qw( getaddrinfo getnameinfo );
use IO::Socket;
my $sock;
my %hints = ( socktype => SOCK_STREAM );
my @res = getaddrinfo( "www.google.com", "www", \%hints );
while( my $ai = shift @res ) {
$sock = IO::Socket->new();
$sock->socket( $ai->{family}, $ai->{socktype}, $ai->{protocol} ) or
undef $sock, next;
$sock->connect( $ai->{addr} ) or undef $sock, next;
last;
}
if( $sock ) {
my ( $host, $service ) = getnameinfo( $sock->peername );
print "Connected to $host:$service\n";
}
like look at her. looping through address structures, trying each one, actually connecting. main character energy fr fr. π
πΈ DESCRIPTION πΈ
so Socket::GetAddrInfo gives you the functions getaddrinfo and getnameinfo. but here's the tea: those functions return lists where the first element is the error value β or something false, which means no error happened. so you're always out here checking that first element yourself like it's your job. π
this module wraps the functions that Socket::GetAddrInfo provides, checks that error value for you, and throws an exception (using die) if an error occurred. and if nothing went wrong? then the remaining values are just returned as normal, no drama, no extra steps. π
and the vibe here is that it can simplify the logic of a program which otherwise simply throws its own exception on failure anyway. like if you were gonna die on failure regardless β bestie, why are you writing the check yourself? let the module do it. periodt.
π¦ FUNCTIONS π¦
β @res = getaddrinfo( $host, $service, $hints )
after a successful lookup, this returns the list of address structures, exactly as documented in Socket::GetAddrInfo. π
but if the lookup fails? an exception containing the string form of the error gets thrown instead. no silent undef, no sneaky false first element β we're throwing hands, we're throwing exceptions, we're not playing. π₯
β ( $host, $service ) = getnameinfo( $addr, $flags, $xflags )
after a successful lookup, this returns the host and service name, as documented in Socket::GetAddrInfo. π
and if the lookup fails β same deal, bestie β an exception containing the string form of the error is thrown instead. π
π AUTHOR π
Paul Evans <leonerd@leonerd.org.uk>
(the legend. the icon. we stan. π«Ά)
and that's the whole page, no cap. this doc came from perl v5.40.0, dated 2025-02-01, and it's filed under Socket...:Strict(3pm). go forth and let your errors throw themselves, bestie πβ¨
π¦ where this page comes from
- debian trixie: libsocket-getaddrinfo-perl 0.22-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libsocket-getaddrinfo-perl 0.22-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libsocket-getaddrinfo-perl 0.22-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms