Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

No need to escape right brace in regex

by syphilis (Archbishop)
on Oct 21, 2014 at 04:13 UTC ( [id://1104517]=perlquestion: print w/replies, xml ) Need Help??

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

In recent versions of blead (including current 5.21.5) I get:
C:\>perl -le "$_ =~ /sub mymy{ }/;" Unescaped left brace in regex is deprecated, passed through in regex; +marked by <-- HERE in m/sub mymy{ <-- HERE }/ at -e line 1. C:\>
That's easily fixed:
C:\>perl -le "$_ =~ /sub mymy\{ }/;" C:\>
But why not also (or instead) deprecate the unescaping of the *right* brace ?

Cheers,
Rob

Replies are listed 'Best First'.
Re: No need to escape right brace in regex
by Athanasius (Archbishop) on Oct 21, 2014 at 04:40 UTC

    I don’t know why, but this behaviour is apparently by design:

    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.

    perl5211delta#Deprecations

    Presumably the planned “future extensions to the language” (whatever they are) won’t be affected by unescaped right braces.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      { is only special if it's followed by /\d+,?\d*}/. If they want to add anything else that can go between { }, they have to either add a simple rule to the language (always backslash { unless you want special behavior), or a bunch of weird rules that they might want to change again later.

      } is never special unless a preceding { makes it so.

        But you don't *always* get the deprecation warning:
        C:\>perl -le "$_ =~ /{}/;" C:\>perl -le "$_ =~ /.+{}/;" C:\>perl -le "$_ =~ /a+{}/;" C:\>perl -le "$_ =~ /(a+)?{}/;" C:\>perl -le "$_ =~ /a{}/;" Unescaped left brace in regex is deprecated, passed through in regex; +marked by <-- HERE in m/a{ <-- HERE }/ at -e line 1. C:\>perl -le "$_ =~ /\d{}/;" Unescaped left brace in regex is deprecated, passed through in regex; +marked by <-- HERE in m/\d{ <-- HERE }/ at -e line 1. C:\>
        That behaviour fits ok with the (undetailed) documentation that Athanasius found, but does it tie in at all with the cases you're describing ?
        (I don't see the connection, though that doesn't mean there isn't one :-)

        Cheers,
        Rob
        Hmm I think it's even more complicated, there is still (?{code}) and (??{code}) to handle.

        But yes looks like they are cleaning up some messi parsing rules.

        Cheers Rolf

        (addicted to the Perl Programming Language and ☆☆☆☆ :)

Re: No need to escape right brace in regex
by biohisham (Priest) on Oct 21, 2014 at 09:30 UTC

    With Perl 5.18.2 on a Ubuntu 14.04 box I don't seem to get any error if I replace the double quotes for single quotes around the code, but when I try to reproduce the behavior in the cases you report (a.k.a escaping the brace or leaving it unescaped ) I get the following error

    perl -le "$_=~/sub mymy\{}/;" syntax error at -e line 1, near ";=~" Execution of -e aborted due to compilation errors.

    The error is consistently reported in all the cases you mentioned in your follow-up reply too


    A 4 year old monk
      Yes, I'm on Windows. I think you really need to stick with single quotes on Ubuntu, otherwise things get a bit weird with the setting and interpolation of $_.
      The Windows shell doesn't interpolate on the "$" sign - as "$" isn't meaningful on Windows like it is on Linux.

      On my Ubuntu-14.04 with perl-5.18.0, the first time I run the command quoted above, it's fine.
      But after that I get errors too.
      sisyphus@sisyphus5-desktop:~$ echo $_ PATH $ perl -le "$_=~/sub mymy\{}/;" # No error here $ echo $_ PATH=~/sub mymy\{}/; $ perl -le "$_=~/sub mymy\{}/;" Backslash found where operator expected at -e line 1, near "mymy\" syntax error at -e line 1, near "mymy\" Execution of -e aborted due to compilation errors.
      The actual error messages can vary a bit, depending upon what gets run when - but I've seen the same error as you reported.

      Cheers,
      Rob
        That's right. The Windows shell does not apply on the "$" sign and is not significant on Windows as it is usually with Linux.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1104517]
Approved by Athanasius
Front-paged by biohisham
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-24 22:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found