http://qs321.pair.com?node_id=59241

ryan has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I've needed to validate user input to ensure it is a validly formatted IPv4 address.

The one I came up with before I looked around was:
/(^(\d{1,3}\.){3})\d{1,3}$/ The double brackets are to ensure the ^ stands for 1 triplet, not all three of them such as  (^\d{1,3}\.){3} which could never occur

Is there anything failsafe yet more efficient than this?

After hunting around I discovered suprisingly that many mail programs and even the LRP favour using this regex:
/^\d+\.\d+\.\d+\.\d+$/ Which will fail on things like 1111.0.0.0

As a side note, I'm sure there is a way to compare two variables to the same regex in one neat expression rather than
if ($var1 =~ /$regex/&&$var2 =~ /$regex/) I guess for an array a foreach can be used, but just for 2 variables only is there a way?

Thanks folks.