http://www.perlmonks.org?node_id=430235

Validate based on whether $ENV{REMOTE_ADDR} is present in a list of IP's or in the IP ranges listed in CIDR notation. Real-world application was within a subfunction, used to restrict signup for an email account.
#!/usr/bin/perl use CGI; use Net::CIDR; open ACL, "< /usr/local/atmail/ACL.txt"; my (@ACL, @CIDRs, $entry, $entry1, $offset, $offset1, $ip, $found); # Create an array of the entries from the list file while(<ACL>){ push(@ACL, $_); } # Push the CIDR's into an array and undef them (avoiding splice() prob +lems with the foreach and offset) in @ACL so they can be easily remov +ed later... foreach $entry (@ACL){ if ($entry =~ /(\d{1,3}\.){3}\d+\//){ push(@CIDRs, $entry); undef $ACL[$offset]; } $offset++; } # Anything left in the list that's not an IP gets whacked foreach $entry1 (@ACL){ if (!(Net::CIDR::cidrvalidate($entry1))){ undef $ACL[$offset1]; } $offset1++; } # Iterate ACL, finishing up if the IP in the HTTP request can be found + in any of the CIDRs or if it matches the current IP in the list. foreach $ip (@ACL){ chomp $ip; if ($ENV{REMOTE_ADDR} eq "$ip" || Net::CIDR::cidrlookup($ENV{REMOT +E_ADDR}, @CIDRs)){ $found = 1; last; } } # If your IP is allowed, do nothing. If not, display error message. if ($found){ } else{ print "Content-Type: text/html\n\n"; print "ERROR YOU CANT SIGNUP"; exit; }