my $name_re = qr/^[- A-Za-z]+$/; ... unless ($name =~ $name_re) { warn "Bad name '$name'"; ... }