bowei_99 has asked for the wisdom of the Perl Monks concerning the following question:

I have the following code:
use strict; use warnings; my $obj_ip = new NetAddr::IP '10.12.100.0','255.255.252.0'; print "Network: " . $obj_ip->network . "\n"; print "Bits: " . $obj_ip->bits . "\n"; print "CIDR: " .$obj_ip->cidr . "\n"; print "Addr: " . $obj_ip->addr . "\n"; print "Mask: " . $obj_ip->mask . "\n";
which gives me the following:
Network: 10.12.100.0/22 Bits: 32 CIDR: 10.12.100.0/22 Addr: 10.12.100.0 Mask: 255.255.252.0
Why does the Bits show as 32? According the cpan documentation, the default is 32, but it doesn't match with what the CIDR, Network and Mask show. Am I missing something, or is this a bug that I have to get around by parsing $ob_ip->cidr?

-- Burvil

Replies are listed 'Best First'.
Re: Bits wrong output in NetAddr::IP ?
by jethro (Monsignor) on Mar 25, 2011 at 15:01 UTC

    Docu says:

    ->bits()
    Returns the width of the address in bits. Normally 32 for v4 and 128 for v6.

    I.e. the complete width of the address, which is 32 for v4 adresses. It does not talk about the network part or the local part of that address.

Re: Bits wrong output in NetAddr::IP ?
by Anonymous Monk on Mar 25, 2011 at 15:09 UTC
    For the names of the parts see Classless Inter-Domain Routing
    #!/usr/bin/perl -- use strict; use warnings; use NetAddr::IP; my $O = NetAddr::IP->new( '10.12.100.0','255.255.252.0'); warn $O->mask(), ' '; warn $O->masklen(), ' '; __END__ 255.255.252.0 at - line 6. 22 at - line 7.