develooper Front page | perl.perl5.porters | Postings from October 2014

Re: Expected behavior of \Q inside a bracketed character class?

Thread Previous | Thread Next
From:
Dave Mitchell
Date:
October 17, 2014 11:05
Subject:
Re: Expected behavior of \Q inside a bracketed character class?
Message ID:
20141017110504.GA5204@iabyn.com
On Tue, Oct 14, 2014 at 02:51:19PM -0400, Marco Moreno wrote:
> Using a \Q inside a bracketed character class, should this return true or
> false?
> 
> perl -e 'print "foo"=~ /\Q[a-z]\E/ ? "true" : "false"'
> 
> It returns "false" for my 5.18.2 and 5.20.0 installs.

You talk about "\Q inside a bracketed character class" but your example
above is actually a bracketed character class inside a \Q. Which one are
you concerned about?

In any case, I would expect that

    /\Q[a-z]\E/

is equivalent to

    /\[a\-z\]/

(which it is), while I would expect that

    /[\Qa-z\E]/

is equivalent to

    /[a\-z]/

(which it is), but which seems to contract the documentation you quote:

> However in perlrebackslash under "All the sequences and escapes" it states:
> 
> 
> Those not usable within a bracketed character class (like [\da-z] ) are
> marked as Not in [].
>   \E                Turn off \Q, \L and \U processing.  Not in [].
>   \Q                Quote (disable) pattern metacharacters till \E.  Not in
> [].
> 
> According to these docs, I would have expected \Q and \E to lose their
> meaning and be ignored, thus returning true instead of false.  What is the
> proper behavior?

So I think the docs are wrong. For example, the following:

    $r = qr/[a\Q]b\E]/;
    print $r,"\n";

outputs

    ...[a\]b]...

at least as far back as 5.6.1




-- 
More than any other time in history, mankind faces a crossroads. One path
leads to despair and utter hopelessness. The other, to total extinction.
Let us pray we have the wisdom to choose correctly.
    -- Woody Allen

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About