Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Catch regular expression errors with eval

Reply
Thread Tools

Catch regular expression errors with eval

 
 
David Liang
Guest
Posts: n/a
 
      09-21-2009
Sorry for the newcomer question; I couldn't find anything related.
What I'd like to do is compile and check if a regular expression is
proper:

$re = '[bad';
eval { $foo = qr/$re/; };
if ($@) {
# ...
}

I was thinking the eval would catch normally fatal errors, but it
doesn't catch the regex error. The program crashes with:

Unmatched [ in regex; marked by <-- HERE in m/[ <-- HERE bad/
at ...

Am I doing anything wrong? Is there a better way to do this?
Thanks.
 
Reply With Quote
 
 
 
 
Uri Guttman
Guest
Posts: n/a
 
      09-21-2009
>>>>> "DL" == David Liang <(E-Mail Removed)> writes:

DL> Sorry for the newcomer question; I couldn't find anything related.
DL> What I'd like to do is compile and check if a regular expression is
DL> proper:

DL> $re = '[bad';
DL> eval { $foo = qr/$re/; };
DL> if ($@) {
DL> # ...
DL> }

DL> I was thinking the eval would catch normally fatal errors, but it
DL> doesn't catch the regex error. The program crashes with:

DL> Unmatched [ in regex; marked by <-- HERE in m/[ <-- HERE bad/
DL> at ...

use eval string instead. eval block catches dies but that is a syntax
error and is fatal. eval string catches syntax errors and you can do the
same check. i don't recommend eval string often but this is one of those
cases where it is useful.

uri
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
issue with eval and regular expression graphicsxp Javascript 7 06-13-2009 06:59 AM
error with regular expression inside eval block Barun Singh Ruby 4 01-29-2009 12:55 PM
Matching abitrary expression in a regular expression =?iso-8859-1?B?bW9vcJk=?= Java 8 12-02-2005 12:51 AM
Errors, errors, errors Mark Goldin ASP .Net 2 01-17-2004 08:05 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments