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