Also if you're working with netmasks (which it looks like you might be), you might find all that you're trying to do has been been done - look into Net::IP and NetAddr::IP.
Update: didn't see that 'new to subroutines' bit at first. Carry on, carry on..