Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I capture "Unrecognized escape" warning when compiling a regex?

Tags:

regex

perl

I'm reading a regular expression from a configuration file, which may or may not have invalid syntax in it. (It's locked down behind a firewall, so let's not get into security.) I've been able to test for a number of errors and give a friendly message.

No such luck on this one:

Unrecognized escape \Q passed through in regex

I know what causes it, I just want to know if I can capture it in Perl 5.8. So far it has resisted my efforts to check for this condition.

So the question is: does anybody know how to capture this? Do I have to redirect STDERR?

like image 758
Axeman Avatar asked Sep 16 '25 17:09

Axeman


1 Answers

You can make the warning FATAL and use block eval:

#!/usr/bin/perl

use strict;
use warnings;

my $s = '\M';

my $r = eval {
    use warnings FATAL => qw( regexp );
    qr/$s/;
};

$r or die "Runtime regexp compilation produced:\n$@\n";
like image 180
Sinan Ünür Avatar answered Sep 18 '25 10:09

Sinan Ünür