use NetAddr::IP; if (defined NetAddr::IP->new($myip)) { ... Your code goes here ... } else { die "Do not feed me with garbage!\n"; }