package AN::Tools::Convert; # # This module contains methods used to convert data between types # use strict; use warnings; use Data::Dumper; our $VERSION = "3.0.0"; my $THIS_FILE = "Convert.pm"; ### Methods; # cidr =pod =encoding utf8 =head1 NAME AN::Tools::Convert Provides all methods related to converting data. =head1 SYNOPSIS use AN::Tools; # Get a common object handle on all AN::Tools modules. my $an = AN::Tools->new(); # Access to methods using '$an->Convert->X'. # # Example using 'cidr()'; my $subnet = $an->Convert->codr({cidr => "24"}); =head1 METHODS Methods in this module; =cut sub new { my $class = shift; my $self = {}; bless $self, $class; return ($self); } # Get a handle on the AN::Tools object. I know that technically that is a sibling module, but it makes more # sense in this case to think of it as a parent. sub parent { my $self = shift; my $parent = shift; $self->{HANDLE}{TOOLS} = $parent if $parent; return ($self->{HANDLE}{TOOLS}); } ############################################################################################################# # Public methods # ############################################################################################################# =head2 cidr This takes an IPv4 CIDR notation and returns the dotted-decimal subnet, or the reverse. # Convert a CIDR notation to a subnet. my $subnet = $an->Convert->cidr({cidr => "24"}); In the other direction; # Convert a subnet to a CIDR notation. my $cidr = $an->Convert->cidr({subnet => "255.255.255.0"}); If the input data is invalid, an empty string will be returned. =head2 Parameters; There are two parameters, each of which is optional, but one of them is required. =head3 cidr (optional) This is a CIDR notation (between 0 and 24) to convert to a dotted-decimal address. =head3 subnet (optional) This is a dotted-decimal subnet to convert to a CIDR notation. =cut sub cidr { my $self = shift; my $parameter = shift; my $an = $self->parent; my $cidr = defined $parameter->{cidr} ? $parameter->{cidr} : ""; my $subnet = defined $parameter->{subnet} ? $parameter->{subnet} : ""; my $output = ""; $an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { cidr => $cidr, subnet => $subnet, }}); if ($cidr) { # Convert a cidr to a subnet if ($cidr eq "0") { $output = "0.0.0.0"; } elsif ($cidr eq "1") { $output = "128.0.0.0"; } elsif ($cidr eq "2") { $output = "192.0.0.0"; } elsif ($cidr eq "3") { $output = "224.0.0.0"; } elsif ($cidr eq "4") { $output = "240.0.0.0"; } elsif ($cidr eq "5") { $output = "248.0.0.0"; } elsif ($cidr eq "6") { $output = "252.0.0.0"; } elsif ($cidr eq "7") { $output = "254.0.0.0"; } elsif ($cidr eq "8") { $output = "255.0.0.0"; } elsif ($cidr eq "9") { $output = "255.128.0.0"; } elsif ($cidr eq "10") { $output = "255.192.0.0"; } elsif ($cidr eq "11") { $output = "255.224.0.0"; } elsif ($cidr eq "12") { $output = "255.240.0.0"; } elsif ($cidr eq "13") { $output = "255.248.0.0"; } elsif ($cidr eq "14") { $output = "255.252.0.0"; } elsif ($cidr eq "15") { $output = "255.254.0.0"; } elsif ($cidr eq "16") { $output = "255.255.0.0"; } elsif ($cidr eq "17") { $output = "255.255.128.0"; } elsif ($cidr eq "18") { $output = "255.255.192.0"; } elsif ($cidr eq "19") { $output = "255.255.224.0"; } elsif ($cidr eq "20") { $output = "255.255.240.0"; } elsif ($cidr eq "21") { $output = "255.255.248.0"; } elsif ($cidr eq "22") { $output = "255.255.252.0"; } elsif ($cidr eq "23") { $output = "255.255.254.0"; } elsif ($cidr eq "24") { $output = "255.255.255.0"; } elsif ($cidr eq "25") { $output = "255.255.255.128"; } elsif ($cidr eq "26") { $output = "255.255.255.192"; } elsif ($cidr eq "27") { $output = "255.255.255.224"; } elsif ($cidr eq "28") { $output = "255.255.255.240"; } elsif ($cidr eq "29") { $output = "255.255.255.248"; } elsif ($cidr eq "30") { $output = "255.255.255.252"; } elsif ($cidr eq "31") { $output = "255.255.255.254"; } elsif ($cidr eq "32") { $output = "255.255.255.255"; } } elsif ($subnet) { if ($subnet eq "0.0.0.0" ) { $output = "0"; } elsif ($output eq "128.0.0.0" ) { $output = "1"; } elsif ($output eq "192.0.0.0" ) { $output = "2"; } elsif ($output eq "224.0.0.0" ) { $output = "3"; } elsif ($output eq "240.0.0.0" ) { $output = "4"; } elsif ($output eq "248.0.0.0" ) { $output = "5"; } elsif ($output eq "252.0.0.0" ) { $output = "6"; } elsif ($output eq "254.0.0.0" ) { $output = "7"; } elsif ($output eq "255.0.0.0" ) { $output = "8"; } elsif ($output eq "255.128.0.0" ) { $output = "9"; } elsif ($output eq "255.192.0.0" ) { $output = "10"; } elsif ($output eq "255.224.0.0" ) { $output = "11"; } elsif ($output eq "255.240.0.0" ) { $output = "12"; } elsif ($output eq "255.248.0.0" ) { $output = "13"; } elsif ($output eq "255.252.0.0" ) { $output = "14"; } elsif ($output eq "255.254.0.0" ) { $output = "15"; } elsif ($output eq "255.255.0.0" ) { $output = "16"; } elsif ($output eq "255.255.128.0" ) { $output = "17"; } elsif ($output eq "255.255.192.0" ) { $output = "18"; } elsif ($output eq "255.255.224.0" ) { $output = "19"; } elsif ($output eq "255.255.240.0" ) { $output = "20"; } elsif ($output eq "255.255.248.0" ) { $output = "21"; } elsif ($output eq "255.255.252.0" ) { $output = "22"; } elsif ($output eq "255.255.254.0" ) { $output = "23"; } elsif ($output eq "255.255.255.0" ) { $output = "24"; } elsif ($output eq "255.255.255.128" ) { $output = "25"; } elsif ($output eq "255.255.255.192" ) { $output = "26"; } elsif ($output eq "255.255.255.224" ) { $output = "27"; } elsif ($output eq "255.255.255.240" ) { $output = "28"; } elsif ($output eq "255.255.255.248" ) { $output = "29"; } elsif ($output eq "255.255.255.252" ) { $output = "30"; } elsif ($output eq "255.255.255.254" ) { $output = "31"; } elsif ($output eq "255.255.255.255" ) { $output = "32"; } } $an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { output => $output }}); return($output); }