IPv4Addr(3)
π¦ IPv4Addr(3) β Net::IPv4Addr, bestie edition π
okay so this is the perl doc for Net::IPv4Addr, which lives over in "User Contributed Perl Documentation" (iykyk) π
β¨ NAME β¨
Net::IPv4Addr β a Perl extension for manipulating IPv4 addresses. that's it, that's the whole vibe π«Ά
πΈ SYNOPSIS πΈ
use Net::IPv4Addr qw( :all );
my ($ip,$cidr) = ipv4_parse( "127.0.0.1/24" );
my ($ip,$cidr) = ipv4_parse( "192.168.100.10 / 255.255.255.0" );
my ($net,$msk) = ipv4_network( "192.168.100.30" );
my $broadcast = ipv4_broadcast( "192.168.100.30/26" );
if ( ipv4_in_network( "192.168.100.0", $her_ip ) ) {
print "Welcome !";
}
etc.
like yes bestie etc. is literally in the docs, we love a casual queen π
π DESCRIPTION π
Net::IPv4Addr gives you functions for parsing IPv4 addresses β both the traditional address/netmask format and the newer CIDR format (iconic, honestly). it also has methods for calculating the network and broadcast address, and for checking whether a given address is in a specific network. main character energy for a module ngl β
π ADDRESSES π
so every Net::IPv4Addr function accepts addresses in many formats. the parsing is very liberal β it's giving "no rules, just vibes" π
all of these? accepted, slay:
127.0.0.1
192.168.001.010/24
192.168.10.10/255.255.255.0
192.168.30.10 / 21
10.0.0.0 / 255.0.0.0
255.255.0.0
those wouldn't though, and honestly? valid of it π
272.135.234.0
192.168/16
most functions accept the address and netmask or masklength in the same scalar value or as separate values. that is, either:
my($ip,$masklength) = ipv4_parse($cidr_str);
my($ip,$masklength) = ipv4_parse($ip_str,$msk_str);
π§ USING π§
no functions are exported by default. none. zero. zip. π so either use the ":all" tag to import them all, or explicitly import the ones you actually need. boundaries, we stan π«Ά
π FUNCTIONS π
π₯ ipv4_parse
my ($ip,$msklen) = ipv4_parse($cidr_str);
my $cidr = ipv4_parse($ip_str,$msk_str);
my ($ip) = ipv4_parse($ip_str,$msk_str);
parses an IPv4 address β in scalar context you get the address in CIDR format, and in array context you get the address and the mask length π
if the parameters don't contain a netmask or a mask length: in scalar context only the IPv4 address comes back, and in array context the mask length is undef. lowkey mysterious of it β¨
if the function cannot parse its input, it croaks π trap it using "eval" if you don't like that.
πΈ ipv4_network
my $cidr = ipv4_network($ip_str);
my $cidr = ipv4_network($cidr_str);
my ($net,$msk) = ipv4_network( $net_str, $msk_str);
in scalar context, this returns the network in CIDR format that the address is in. in array context, it returns the network address and its mask length as a two-element array π¦ if the input is a host without a netmask or mask length, the default netmask is assumed.
again β the function croaks if the input is invalid. it's sensitive, okay? π
π ipv4_broadcast
my ($broadcast) = ipv4_broadcast($ip_str);
my $broadcast = ipv4_broadcast($ip_str,$msk_str);
returns the broadcast address π‘ if the input doesn't contain a netmask or mask length, the default netmask is assumed.
this function croaks if the input is invalid. no cap π«Ά
π¦ ipv4_network (yes, again π )
my $cidr = ipv4_network($net_str);
my $cidr = ipv4_network($cidr_sstr);
my ($net,$msk) = ipv4_network( $ip_str, $mask_str);
okay so yes, ipv4_network shows up twice in this doc β not a glitch, it's literally what the page says, we keep it π (and $cidr_sstr is spelled exactly like that in the original too. character for character, bestie.)
in scalar context, this returns the network in CIDR format that the address is in. in array context, it returns the network address and its mask length as a two-element array π if the input is a host without a netmask or mask length, the default netmask is assumed.
again, the function croaks if the input is invalid. consistent queen π
π ipv4_in_network
print "Yes" if ipv4_in_network( $cidr_str1, $cidr_str2);
print "Yes" if ipv4_in_network( $ip_str1, $mask_str1, $cidr_str2 );
print "Yes" if ipv4_in_network( $ip1, $mask1, $ip2, $msk2 );
this one checks if the second network is contained in the first one, and it implements the following semantics, get ready:
- if
net1ornet2is a magic address (0.0.0.0or255.255.255.255) then this function returns true β¨ - if
net1is a host,net2will be in the same net only if it is the same host - if
net2is a host, it will be contained innet1only if it is part ofnet1 - if
net2is only part ofnet1if it is entirely contained innet1
trap bad input with "eval" or else π
π ipv4_checkip
if ($ip = ipv4_checkip($str) ) {
# Do something
}
returns the IPv4 address found in the string, or undef if the input doesn't contain a valid IPv4 address π a simple queen, honestly.
π§ ipv4_cidr2msk
my $netmask = ipv4_cidr2msk( $cidr );
returns the netmask corresponding to the mask length given in input. as usual, croaks if it doesn't like your input π (in this case, a number between 0 and 32 β that's the range, bestie, don't push it).
β ipv4_msk2cidr
my $masklen = ipv4_msk2cidr( $msk );
returns the mask length of the netmask in input. as usual, croaks if it doesn't like your input π
π AUTHOR π
Francis J. Lacoste <francis.lacoste@iNsu.COM> β ty for your service king π«Ά
π COPYRIGHT π
Copyright (c) 1999, 2000 iNsu Innovations Inc. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the terms as perl itself. (yes, "as perl itself" β exactly like that π)
π¦ SEE ALSO π¦
perl(1) and ipv4calc(1) β go read those too, bestie, expand the brain β¨
okay and that's a wrap, babes π this page came to you courtesy of perl v5.42.0, dated 2024-03-04, filed under IPv4Addr(3) β mwah, stay parsed ππ π«Ά
π¦ where this page comes from
- opensuse-leap 16.0: perl-Net-IPv4Addr 0.10-bp160.148.11 Β· GPL-2.0+
- opensuse-leap 16.0: perl-Net-IPv4Addr 0.10-bp160.148.11 Β· GPL-2.0+
- opensuse-leap 16.0: perl-Net-IPv4Addr 0.10-bp160.148.11 Β· GPL-2.0+
- opensuse-leap 16.0: perl-Net-IPv4Addr 0.10-bp160.148.11 Β· GPL-2.0+
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms