1Net::Works::Address(3)User Contributed Perl DocumentationNet::Works::Address(3)
2
3
4
6 Net::Works::Address - An object representing a single IP (4 or 6)
7 address
8
10 version 0.22
11
13 use Net::Works::Address;
14
15 my $ip = Net::Works::Address->new_from_string( string => '192.0.2.1' );
16 print $ip->as_string(); # 192.0.2.1
17 print $ip->as_integer(); # 3221225985
18 print $ip->as_binary(); # 4-byte packed form of the address
19 print $ip->as_bit_string(); # 11000000000000000000001000000001
20 print $ip->version(); # 4
21 print $ip->prefix_length(); # 32
22
23 my $next = $ip->next_ip(); # 192.0.2.2
24 my $prev = $ip->previous_ip(); # 192.0.2.0
25
26 if ( $next > $ip ) { print $ip->as_string(); }
27
28 my @sorted = sort $next, $prev, $ip;
29
30 my $ipv6 = Net::Works::Address->new_from_string( string => '2001:db8::1234' );
31 print $ipv6->as_integer(); # 42540766411282592856903984951653831220
32
33 my $ip_from_int = Net::Works::Address->new_from_integer(
34 integer => "42540766411282592856903984951653831220"
35 );
36
38 Objects of this class represent a single IP address. It can handle both
39 IPv4 and IPv6 addresses. It provides various methods for getting
40 information about the address, and also overloads the objects so that
41 addresses can be compared as integers.
42
43 For IPv6, it uses 128-bit integers (via Math::Int128) to represent the
44 numeric value of an address.
45
47 This class provides the following methods:
48
49 Net::Works::Address->new_from_string( ... )
50 This method takes a "string" parameter and an optional "version"
51 parameter. The "string" parameter should be a string representation of
52 an IP address.
53
54 The "version" parameter should be either 4 or 6, but you don't really
55 need this unless you're trying to force a dotted quad to be interpreted
56 as an IPv6 address or to a force an IPv6 address colon-separated hex
57 number to be interpreted as an IPv4 address.
58
59 Net::Works::Address->new_from_integer( ... )
60 This method takes a "integer" parameter and an optional "version"
61 parameter. The "integer" parameter should be an integer representation
62 of an IP address.
63
64 The "version" parameter should be either 4 or 6. Unlike with strings,
65 you'll need to set the version explicitly to get an IPv6 address.
66
67 $ip->as_string()
68 Returns a string representation of the address in the same format as
69 inet_ntop, e.g., "192.0.2.1", "::192.0.2.1", or "2001:db8::1234".
70
71 $ip->as_integer()
72 Returns the address as an integer. For IPv6 addresses, this is returned
73 as a Math::Int128 object, regardless of the value.
74
75 $ip->as_binary()
76 Returns the packed binary form of the address (4 or 16 bytes).
77
78 $ip->as_bit_string()
79 Returns the address as a string of 1's and 0's, like
80 "00000000000000000000000000010000".
81
82 $ip->as_ipv4_string()
83 This returns a dotted quad representation of an address, even if it's
84 an IPv6 address. However, this will die if the address is greater than
85 the max value of an IPv4 address (2**32 - 1). It's primarily useful for
86 debugging.
87
88 $ip->version()
89 Returns a 4 or 6 to indicate whether this is an IPv4 or IPv6 address.
90
91 $ip->prefix_length()
92 Returns the prefix length for the IP address, which is either 32 (IPv4)
93 or 128 (IPv6).
94
95 $ip->bits()
96 An alias for "$ip->prefix_length()". This helps make addresses &
97 network objects interchangeable in some cases.
98
99 $ip->next_ip()
100 Returns the numerically next IP, regardless of whether or not it's in
101 the same subnet as the current IP.
102
103 This will throw an error if the current IP address it the last address
104 in its IP range.
105
106 $ip->previous_ip()
107 Returns the numerically previous IP, regardless of whether or not it's
108 in the same subnet as the current IP.
109
110 This will throw an error if the current IP address it the first address
111 in its IP range (address 0).
112
114 This class overloads comparison, allowing you to compare two objects
115 and to sort them (either as numbers or strings).
116
117 It also overloads stringification to call the "$ip->as_string()"
118 method.
119
121 • Dave Rolsky <autarch@urth.org>
122
123 • Greg Oschwald <oschwald@cpan.org>
124
125 • Olaf Alders <oalders@wundercounter.com>
126
128 This software is copyright (c) 2016 by MaxMind, Inc.
129
130 This is free software; you can redistribute it and/or modify it under
131 the same terms as the Perl 5 programming language system itself.
132
133
134
135perl v5.32.1 2021-01-27 Net::Works::Address(3)