There's no need to actually use it in the eval, and you can save off the result and use it after the check.
sub validate_re { my $re = shift; eval { $re = qr/$re/ }; return $@ ? undef : $re }
That will return either an already compiled regexp or undef (and $@ will indicate what was wrong in that case).
In reply to Re^2: Test RegEx Validity
by Fletch
in thread Test RegEx Validity
by aplonis
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |