LanX has asked for the wisdom of the Perl Monks concerning the following question:

While migrating code from 5.16 to 5.24 I noticed this deprecation warning to be inconsistent

according to perldelta all literal left braces should be escaped now.

A literal "{" should now be escaped in a pattern

If you want a literal left curly bracket (also called a left brace) in a regular expression pattern, you should now escape it by either preceding it with a backslash ("\{") or enclosing it within square brackets "[{]" , or by using \Q; otherwise a deprecation warning will be raised. This was first announced as forthcoming in the v5.16 release; it will allow future extensions to the language to happen.

but the following code only warns for the second left brace one. Not escaping the first one doesn't throw a warning.

Am I missing something or is this a bug?

DB<31> (my $x="{{abc}}") =~ s/{{/[[/g; print $x Unescaped left brace in regex is deprecated, passed through in regex; +marked by <-- HERE in m/{{ <-- HERE / at (eval 40)[c:/Perl_524/lib/pe +rl5db.pl:737] line 2. + at (eval 40)[c:/Perl_524/lib/perl5db +.pl:737] line 2. eval 'no strict; ( +$@, $!, $^E, $,, $/, $\\, $^W) = @DB::saved;package main; $^D = $^D | + $DB::db_stop; (my +$x="{{abc}}") =~ s/{{/[[/g; print $x; + ' called at c:/Perl_524/lib/perl5db.pl line 737 + DB::eval called at c:/Perl_524/lib/perl5db. +pl line 3131 DB::DB called at -e line 1 + [[abc}} + + DB<32> (my $x="{{abc}}") =~ s/{\{/[[/g; print $x + [[abc}}

PS: sorry for the missing \n in the code, but copying from win-cmd is a PITA

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!

Replies are listed 'Best First'.
Re: Only sometimes deprecated? "Unescaped left brace in regex is deprecated"
by haukex (Archbishop) on Jun 02, 2017 at 17:03 UTC

    Quoting Karl Williamson from #128213 for perl5: No deprecation warning on literal left curly bracket in /.{/ etc:

    So I tried to raise the warning only where the left brace could, with our plans, mean something other than a literal left brace. It turns out that there is lots of code like qr/{..../. That '{', since it's the first thing, can only be a left brace. And similarly in qr/^{.../ or qr/ ... ({...) .../, it can only be a left brace. So there is no need to disturb code where there is not going to be ambiguity. And this cuts down the amount of disruption significantly.

    And quoting the 5.26.0 perldiag entry for "Unescaped left brace in regex is deprecated here (and will be fatal in Perl 5.30), passed through in regex":

    ... The contexts where no warnings or errors are raised are:

    • as the first character in a pattern, or following ^ indicating to anchor the match to the beginning of a line.
    • ...

    Perhaps also relevant: #131352 for perl5: Unescaped literal "{" characters appear not always to be illegal.

      thank you for digging that out, it's much clearer now :)

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

Re: Only sometimes deprecated? "Unescaped left brace in regex is deprecated"
by ikegami (Patriarch) on Jun 02, 2017 at 16:30 UTC

    I think it was done that way intentionally, so it's simply a case of perldelta being a bit imprecise.

    The reason for escaping { is to detect problems with improperly coded {} quantifier, and to allow \X to be extended to \X{...}. (For example, there is now \b, \b{gcb}, \b{wb} and \b{sb}.) It's not likely a bug to have a leading {, and it can't be a \X argument, so there's no real reason to deprecate it.

      Thanks.

      > simply a case of perldelta being a bit imprecise.

      Actually not a bit, just imprecise.

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

Re: Only sometimes deprecated? "Unescaped left brace in regex is deprecated"
by Eily (Monsignor) on Jun 02, 2017 at 16:14 UTC

    My versions of perl don't have the error so I can't test, but I do notice that in your example the first curly bracket is also the first character of the regex, where it can't be the start of a quantifier. So maybe there are still some special cases (like - and ] don't have to be escaped when coming first in a character class), where { is considered unambiguous. This would limit future extensions to cases where {THING} syntax only works as a modifier of the previous token (maybe TOKEN{EXPR} where EXPR validates the current repeat count, for consistency).

      Thanks! :)

      hmm, the perldelta doesn't talk about quantifiers, and the ambiguity would already have existed before.

      I thought it's deprecated to facilitate parsing extended syntax like /abc(?{print "Hi Mom!";})def/

      see perlretut for other examples

      so I'm not sure if it's about quantifiers...

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!

        Because (? is invalid syntax on its own, in any (?X pattern the X is unambiguously special or incorrect, but never litteral. perlre however does talk about extending the quantifier syntax.

        like making the lower bound of a quantifier optional
        And any new syntax that's not a modifier can be added with a (?X pattern. That's why I concluded that disallowing unescaped litteral { was probably an opening toward new quantifier syntax (because { only introduces quantifiers). I thought of something like /.{{!($_ % 3)}}/ instead of /(?:.{3})*/ (it's harder to read though ...), or even "3ABCD" =~ /(\d)(.{{$1}})(.*)/; #(3,"ABC", "D"). But that's wild guesswork

        PS: How do you like all those unbalanced left tokens? ^^"

Re: Only sometimes deprecated? "Unescaped left brace in regex is deprecated"
by Corion (Patriarch) on Jun 02, 2017 at 16:22 UTC

    I think the deprecation counts for all left braces, but the warning/error doesn't happen everywhere yet.