Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: Style question: regex versus string builtin function

by lidden (Curate)
on Oct 02, 2007 at 12:03 UTC ( [id://642084] : note . print w/replies, xml ) Need Help??


in reply to Re: Style question: regex versus string builtin function
in thread Style question: regex versus string builtin function

But 'pattern' is not an empty string after using \Q.

Replies are listed 'Best First'.
Re^3: Style question: regex versus string builtin function
by thospel (Hermit) on Oct 02, 2007 at 13:01 UTC
    Silly enough this still counts as empty. In general I think the way empty regexes work is just bad design. It should only trigger if the regex is empty at the literal code level, not after all kinds of expansion has been done on the stuff between the delimiters.
Re^3: Style question: regex versus string builtin function
by kyle (Abbot) on Oct 02, 2007 at 13:55 UTC

    A \Q does not "fill" an empty regex.

    use Test::More 'tests' => 5; ok( 'foo' =~ //, 'empty regex matches' ); ok( 'foo' =~ /foo/, '/foo/ matches' ); ok( !('bar' =~ //), 'repeated match of foo' ); ok( !('bar' =~ /\Q/), 'repeated match with \\Q' ); my $empty = ''; ok( !('bar' =~ /\Q$empty/), 'interpolated empty string same as \\Q' );
      Well, dang!

      I'd recommended using index because of (inter alia) some gnarly behavior with empty patterns. But it turns out it was even more gnarly than I thought.

      BTW, perlreref says

      If 'pattern' is an empty string, the last I matched regex is used.
      What is the 'I' in 'I matched'? I couldn't find it mentioned again in perlre or perlreref, and searching on 'I' turned out to be ... overproductive.

      throop

        A bug in perlreref? On my screen, I see:

        If ’pattern’ is an empty string, the last I<successfully> matched

        I know too little about POD to say whether this is a problem with the web rendering or a problem with how the file is written.