use NetAddr::IP; push @space, new NetAddr::IP->new($_) for <DATA>; for my $netblock (NetAddr::IP::compact @space) { for (my $ip = $netblock->first; $ip <= $netblock->last; $ip++) { # Do something with $ip } } __DATA__ 10.0.0.0/16 172.16.0.0/24