Net::W...Address(3pm)

πŸ“¦
version 0.22 Β· 5 versions
, in debian bookworm, debian trixie, ubuntu jammy, ubuntu noble, ubuntu resolute, ubuntu stonking

✨ Net::Works::Address ✨

okay bestie, so this is a page from the User Contributed Perl Documentation, section 3pm, and it's literally all about Net::Works::Address. pull up a chair, grab a boba, we're doing IP addresses today πŸ’…


🌸 NAME 🌸

Net::Works::Address β€” it's an object that represents one single IP address, and yeah, it does both IPv4 and IPv6, no cap πŸ‘‘


πŸŽ€ VERSION πŸŽ€

version 0.22 ✨


πŸ¦‹ SYNOPSIS πŸ¦‹

here's the vibe, bestie:

use Net::Works::Address;

my $ip = Net::Works::Address->new_from_string( string => '192.0.2.1' );
print $ip->as_string();     # 192.0.2.1
print $ip->as_integer();    # 3221225985
print $ip->as_binary();     # 4-byte packed form of the address
print $ip->as_bit_string(); # 11000000000000000000001000000001
print $ip->version();       # 4
print $ip->prefix_length();   # 32

my $next = $ip->next_ip();     # 192.0.2.2
my $prev = $ip->previous_ip(); # 192.0.2.0

if ( $next > $ip ) { print $ip->as_string(); }

my @sorted = sort $next, $prev, $ip;

my $ipv6 = Net::Works::Address->new_from_string( string => '2001:db8::1234' );
print $ipv6->as_integer(); # 42540766411282592856903984951653831220

my $ip_from_int = Net::Works::Address->new_from_integer(
    integer => "42540766411282592856903984951653831220"
);

like… the range on this thing? unmatched. we love a versatile icon 🫢


πŸ’œ DESCRIPTION πŸ’œ

so objects of this class are each one single IP address. it handles IPv4 and IPv6, because it contains multitudes ✨. it gives you a whole lil toolkit of methods to get info about the address, and it also overloads the objects so you can compare addresses as integers. main character behavior, fr fr.

for IPv6, it uses 128-bit integers (via Math::Int128) to hold the numeric value of an address. yes bestie, big numbers. huge. πŸ’€


πŸ‘‘ METHODS πŸ‘‘

this class provides the following methods, and every single one of them is giving:

🌟 Net::Works::Address->new_from_string( ... )

takes a "string" parameter, plus an optional "version" parameter. the "string" should be a string representation of an IP address, obviously.

the "version" should be either 4 or 6, but lowkey you don't really need it unless you're trying to force a dotted quad to be read as an IPv6 address, or force an IPv6 address that's a colon-separated hex number to be read as an IPv4 address. niche use case, but slay.

🌟 Net::Works::Address->new_from_integer( ... )

takes an "integer" parameter and an optional "version" parameter. the "integer" should be an integer representation of an IP address.

the "version" should be either 4 or 6. and listen β€” unlike with strings, here you gotta set the version explicitly if you want an IPv6 address. no shortcuts, bestie 😀

🌟 $ip->as_string()

returns a string representation of the address, formatted the same way as inet_ntop, like "192.0.2.1", "::192.0.2.1", or "2001:db8::1234". cute, readable, iconic πŸ’–

🌟 $ip->as_integer()

returns the address as an integer. and for IPv6 addresses? it comes back as a Math::Int128 object, no matter what the value is. every time. periodt.

🌟 $ip->as_binary()

returns the packed binary form of the address β€” either 4 or 16 bytes. compact queen πŸŽ€

🌟 $ip->as_bit_string()

returns the address as a string of 1's and 0's, like "00000000000000000000000000010000". it's giving binary realness πŸ’…

🌟 $ip->as_ipv4_string()

this one hands you a dotted quad representation of an address, even if it's an IPv6 address. but heads up β€” this will die if the address is greater than the max value of an IPv4 address (2**32 - 1). it's primarily useful for debugging. so like, know before you go, bestie πŸ™ˆ

🌟 $ip->version()

returns a 4 or a 6 to tell you whether this is an IPv4 or an IPv6 address. vibe check complete βœ…

🌟 $ip->prefix_length()

returns the prefix length for the IP address, which is either 32 (IPv4) or 128 (IPv6). one or the other, never in between ✨

🌟 $ip->bits()

an alias for $ip->prefix_length(). this helps make addresses and network objects interchangeable in some cases, which is honestly so thoughtful of it 🫢

🌟 $ip->next_ip()

returns the numerically next IP β€” regardless of whether or not it's in the same subnet as the current IP. no boundaries, we simply go up πŸ“ˆ

careful tho: this will throw an error if the current IP address is the last address in its IP range. you've been warned, bestie 😭

🌟 $ip->previous_ip()

returns the numerically previous IP β€” again, regardless of whether or not it's in the same subnet as the current IP. going down, gracefully 🌈

also careful: this will throw an error if the current IP address is the first address in its IP range (address 0). the edge of the universe, basically πŸ’€

πŸ”₯ OVERLOADING πŸ”₯

this class overloads comparison, so you can compare two objects and sort them β€” either as numbers or as strings. sorting? handled. comparing? handled. we love an overachiever πŸ‘‘

it also overloads stringification to call the $ip->as_string() method. so when you just use it like a string, it knows what to do. intuitive, iconic, unmatched πŸ’…


πŸ’– AUTHORS πŸ’–

  • Dave Rolsky <autarch@urth.org>
  • Greg Oschwald <oschwald@cpan.org>
  • Olaf Alders <oalders@wundercounter.com>

three legends, one module. respect the architects 🫑


this software is copyright (c) 2016 by MaxMind, Inc.

this is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. so like, go forth and remix, bestie πŸ’–


and that's the whole page, fr fr β€” brought to you by perl v5.32.0, dated 2021-01-09, straight from the Net::Works::Address(3pm) era. stay iconic, go read your other manpages, mwah πŸ’‹βœ¨πŸŒΈ

πŸ“¦ where this page comes from