use constant REGEXP_HOSTNAME      => qr/^([\w\-.]+)$/; use constant REGEXP_IPADDR        => qr/^((?:(?:25[0-5]|2[0-4]\d|[0-1]??\d{1,2})[.](?:25[0-5]|2[0-4]\d|[0-1]??\d{1,2})[.](?:25[0-5]|2[0-4]\d|[0-1]??\d{1,2})[.](?:25[0-5]|2[0-4]\d|[0-1]??\d{1,2})))$/; sub valid_hostname {   my $value = shift;   return 0 unless $value =~ REGEXP_HOSTNAME; return 0 unless index($value, ".") > 0;   my @parts = split/\./, $value;   my $valid = @parts;   foreach my $label (@parts)   {      $valid = 0 if (length $label > 63) or ($label =~ m!^\-!) or ($label =~ m!\-$!);      last unless $valid;   }   $valid = 0 if $parts[-1] =~ m!^\d.*!;   return $valid; } sub valid_ipaddr {   my $value = shift;   return $value =~ REGEXP_IPADDR; } sub valid_host {   my $value = shift;   return valid_hostname($value) || valid_ipaddr($value); }