in reply to How to identify invalid reg. expr.?

Try something like this
my $format = "foo (bar] baz"; eval '$regex = qr/$format/;'; die "ack - $@" if $@; __output__ ack - Unmatched ( before HERE mark in regex m/foo ( << HERE bar] baz/ +at (eval 1) line 1.
Using the string form of eval() will do runtime compilation of the regex and therefore not break the program if it is invalid. Also note the abbreviation of 'regular expression' is commonly known as regex (and it's a lot easier to pronounce too ;-)
HTH

_________
broquaint

Replies are listed 'Best First'.
• DANGER! Re: Re: How to identify invalid reg. expr.?
by merlyn (Sage) on Jun 05, 2002 at 15:45 UTC
    my $format = "foo (bar] baz"; eval '$regex = qr/$format/;'; die "ack - $@" if $@;
    Danger! Danger! If $format contains a slash, that ends the regex and begins Perl again.

    I think you were trying for this:

    my $format = "foo (bar] baz"; eval { qr/$format/ }; die "ack - $@" if $@;
    Which won't matter if it contains slashes. Almost every code that contains string-eval is broken, and almost never necessary. {grin}

    -- Randal L. Schwartz, Perl hacker


    update: Oh duh. Didn't see the single quotes. In which case, it's overkill, but not dangerous as-is, except that it can be misleading as to why you used single quotes and run-time eval-string instead of compile-time eval-block.