Another insane perl snippet

Posted by Chris on December 3rd, 2008 filed in programming

This finds out whether an ip address is within a certain range.

example:

between(“192.168.1.29″,”192.168.1.1″,”192.168.1.99”); returns true

between(“192.168.1.29″,”10.1.1.1″,”10.1.1.254”); returns false

sub between
{
my ($tgt,$small,$big)[email protected]_;
if($tgt!~m/\./)
{
if($tgt >= $small and $tgt <= $big){return 1;}
else{return 0;}
}
my @tg=split(/\./,$tgt); my $t=shift(@tg); $tgt=join(‘.’,@tg);
my @sm=split(/\./,$small); my $s=shift(@sm); $small=join(‘.’,@sm);
my @bg=split(/\./,$big); my $b=shift(@bg); $big=join(‘.’,@bg);
if($t!=$s or $t!=$b){return 0;}
else{return &between($tgt,$small,$big);}
}

Recursion is fun!

Leave a Comment