in reply to Hash/Array of Regular Expressions?
If you have many, many different patterns to define, you might want to eval them into the hash, like so:my %validate = ( int => sub { my($v)=@_; $v=~/^[0-9]+$/; }, date => sub { my($v)=@_; $v=~/^[12]\d\d\d\-[01]?\d\-[0123]?\d/ +; }, ); foreach my $value (qw [ 24 140 510 2001-04-14 3014-30-55 ]) { print "$value\n"; foreach my $type (sort keys %validate) { print "\tIs '$type'\n" if ($validate{$type}($value)); } }
Of course, taking special care to ensure $regex was a self-contained regex (i.e. /x/ or !/!) and did not contain anything that was going to be invalid when eval'd, though of course you can always check $@ and see what went wrong.$validate{$new_type} = eval "sub { my(\$v)=\@_; \$v=~$regex; }";
|
|---|