sockaddr_in#

Dual-mode shortcut: in scalar context it behaves like pack_sockaddr_in; in list context it behaves like unpack_sockaddr_in.

Synopsis#

my $sin            = sockaddr_in($port, $packed_ipv4);
my ($port, $addr)  = sockaddr_in($sockaddr);

What you get back#

The same values as the underlying pack_sockaddr_in or unpack_sockaddr_in, depending on calling context.

Examples#

# Packing
connect($sock, sockaddr_in(80, inet_aton("127.0.0.1"))) or die $!;
# Unpacking
my ($port, $ip) = sockaddr_in(getpeername($sock));

Edge cases#

  • The dispatch is on calling context, not argument count. In list context exactly one argument is required, in scalar context exactly two (or the deprecated six-argument form); any other count croaks with a usage message, so my ($p, $a) = sockaddr_in($port, $ip) dies.

  • Prefer the explicit pack_sockaddr_in / unpack_sockaddr_in in new code; this name exists for compatibility with older Perl idioms.

Differences from upstream#

Fully compatible with upstream Socket.

See also#

  • pack_sockaddr_in, unpack_sockaddr_in - the explicit forms.

  • sockaddr_in6 - IPv6 counterpart with the same dual behaviour.