in reply to An efficient way to do this?
See Net::IP for dealing with ranges of ips without all those nested loops. And Re^4: How to improve memory usage in this script?? for an example of usage that may or may not be applicable to your situation.