inet_aton#

Turn a dotted-quad string or a hostname into a 4-byte packed IPv4 address, or undef if the name cannot be resolved.

Synopsis#

my $packed = inet_aton("127.0.0.1");
my $packed = inet_aton("localhost");

What you get back#

A 4-byte string in network byte order suitable as the second argument to pack_sockaddr_in, or undef on failure. To see the bytes as a human-readable address, pass the result through inet_ntoa.

Examples#

my $loop = inet_aton("127.0.0.1");       # 4-byte string
print join(".", unpack("C4", $loop));    # 127.0.0.1
my $sin = pack_sockaddr_in(80, inet_aton("example.com"));

## pass $sin to connect()

Edge cases#

  • A literal IPv4 address is parsed directly — no DNS lookup.

  • A non-resolvable name returns undef.

  • IPv6 addresses are rejected here; use inet_pton(AF_INET6, ...).

Differences from upstream#

Fully compatible with upstream Socket.

See also#

  • inet_ntoa — reverse direction, packed bytes to dotted-quad string.

  • inet_pton — same idea but parameterised by address family.

  • pack_sockaddr_in — the immediate consumer of the packed address.

  • getaddrinfo — richer resolver when you also need a port or IPv6.