Validation of user provided regex

G

Gunnar Hjalmarsson

In an app where a user may provide a Perl regular expression I want to
validate the regex before it's applied. Currently I'm doing something like:

{
local $SIG{__WARN__} = sub { die $_[0] };
eval { $regex = qr($regex) };
die $@ if $@;
}

In other words I rely on Perl's ability to catch errors, where also
warnings are treated as errors.

Are there other appropriate checks that could be done?
 
A

anno4000

Gunnar Hjalmarsson said:
In an app where a user may provide a Perl regular expression I want to
validate the regex before it's applied. Currently I'm doing something like:

{
local $SIG{__WARN__} = sub { die $_[0] };
eval { $regex = qr($regex) };
die $@ if $@;
}

In other words I rely on Perl's ability to catch errors, where also
warnings are treated as errors.

Are there other appropriate checks that could be done?

If code interpolations are a possibility, you may want to "use re
'eval';" in the block. Or not, depending...

Anno
 
G

Gunnar Hjalmarsson

Gunnar Hjalmarsson said:
In an app where a user may provide a Perl regular expression I want to
validate the regex before it's applied. Currently I'm doing something like:

{
local $SIG{__WARN__} = sub { die $_[0] };
eval { $regex = qr($regex) };
die $@ if $@;
}

In other words I rely on Perl's ability to catch errors, where also
warnings are treated as errors.

Are there other appropriate checks that could be done?

If code interpolations are a possibility, you may want to "use re
'eval';" in the block. Or not, depending...

Thanks for the tip, but I really don't see a need for using (?{ ... }),
so I guess I'd better not.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top