valid_dotted_quad takes a list of IP addresses and returns true if all are well-formed, false otherwise
sub valid_dotted_quad { my $all_valid = 1; my @sections; ARGUMENT:for (@_) { next ARGUMENT unless $all_valid; next ARGUMENT unless ($all_valid &&= /^\d{1,3}\.\d{1,3 +}\.\d{1,3} \.\d{1,3}$/o); @sections = split /\./, $_; for (@sections) { next ARGUMENT unless ($all_valid &&= ($_ <= 0x +FF)); } } return $all_valid; }

Replies are listed 'Best First'.
RE: Check IP addresses for good form
by knight (Friar) on Aug 16, 2000 at 20:16 UTC
    Don't roll your own:
    use Net::IPv4Addr qw( :all ); if ($ip = ipv4_checkip($str)) { # do something }
    This also handles IP+netmask strings like "123.45.67.89/255.255.255.0".
RE: Check IP addresses for good form
by merlyn (Sage) on Aug 16, 2000 at 17:47 UTC
    Unfortunately, this doesn't allow for all valid forms of an IP address, including such things as 127.1. See the Socket manpage for the function inet_aton on which to base such a function.

    -- Randal L. Schwartz, Perl hacker

(ar0n) Same thing, one line
by ar0n (Priest) on Aug 16, 2000 at 18:26 UTC