Net::IPv4Addr(3) Perl extension for manipulating IPv4 addresses.


use Net::IPv4Addr qw( :all );
my ($ip,$cidr) = ipv4_parse( "" );
my ($ip,$cidr) = ipv4_parse( " /" );
my ($net,$msk) = ipv4_network( "" );
my $broadcast = ipv4_broadcast( "" );
if ( ipv4_in_network( "", $her_ip ) ) {
print "Welcome !";


Net::IPv4Addr provides functions for parsing IPv4 addresses both in traditional address/netmask format and in the new CIDR format. There are also methods for calculating the network and broadcast address and also to check if a given address is in a specific network.


All of Net::IPv4Addr functions accept addresses in many formats. The parsing is very liberal.

All these addresses would be accepted: / 21 /

Those wouldn't though:

Most functions accepts 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);


No functions are exported by default. Either use the ":all" tag to import them all or explicitly import those you need.


    my ($ip,$msklen) = ipv4_parse($cidr_str);
    my $cidr = ipv4_parse($ip_str,$msk_str);
    my ($ip) = ipv4_parse($ip_str,$msk_str);

Parse an IPv4 address and return in scalar context the address in CIDR format, in an array context the address and the mask length.

If the parameters doesn't contains a netmask or a mask length, in scalar context only the IPv4 address is returned and in an array context the mask length is undefined.

If the function cannot parse its input, it croaks. Trap it using "eval" if you don't like that.

    my ($broadcast) = ipv4_broadcast($ip_str);
    my $broadcast   = ipv4_broadcast($ip_str,$msk_str);

This function 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.

    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 function returns the network in CIDR format in which the address is. In array context, it returns the network address and its mask length as a two elements 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.

    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 function checks if the second network is contained in the first one and it implements the following semantics :

   If net1 or net2 is a magic address ( or
   then this function returns true.
   If net1 is a host, net2 will be in the same net only if
   it is the same host.
   If net2 is a host, it will be contained in net1 only if
   it is part of net1.
   net2 is only part of net1 if it is entirely contained in

Trap bad input with "eval" or else.

    if ($ip = ipv4_chkip($str) ) {
        # Do something

Return the IPv4 address in the string or undef if the input doesn't contain a valid IPv4 address.

    my $netmask = ipv4_cidr2msk( $cidr );

Returns the netmask corresponding to the mask length given in the input. As usual, croaks if it doesn't like your input (in this case a number between 0 and 32).

    my $masklen = ipv4_msk2cidr( $msk );

Returns the mask length of the netmask in the input. As usual, croaks if it doesn't like your input.


Francis J. Lacoste <[email protected]>


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.