develooper Front page | perl.perl5.porters | Postings from June 2021

Re: qr'$foo' and unescaped $ signs

Thread Previous | Thread Next
From:
Nicholas Clark
Date:
June 30, 2021 15:24
Subject:
Re: qr'$foo' and unescaped $ signs
Message ID:
20210630152353.GK11066@etla.org
On Wed, Jun 30, 2021 at 03:05:04PM +0200, Eirik Berg Hanssen wrote:
> On Wed, Jun 30, 2021 at 12:42 PM Nicholas Clark <nick@ccl4.org> wrote:
> 
> > It seems that if $ is matched, it needs to be the (logical) end of the
> > pattern, else the entire pattern matches. By "logical", I mean that other
> > text follow in the regex, but it needs to be other alternations, or
> > something
> > else that doesn't contribute to matching. It can't be more characters, even
> > if they are present on the next line.
> >
> 
>   You can match more characters, but you need to consume the newline
> first.  (The $ is a zero-width lookahead assertion – it does not consume
> anything.)

D'oh! Thanks. Yes. That was what I goofed.


So, based on your suggestion I've written what I think is a fix, and
pushed it to https://github.com/nwc10/perl5/tree/DD-84569

The pure-Perl implementation is this:

        $pat =~ s <
                     (\\.)           # anything backslash escaped
                   | (\$)(?![)|]|\z) # any unescaped $, except $| $) and end
                   | /               # any unescaped /
                  >
                  {
                      $1 ? $1
                          : $2 ? '${\q($)}'
                          : '\\/'
                  }gex;


The C implementation might actually be easier to read. :-)

It passes tests on blead. I've not *yet* tested it against older Perls,
so I'm not going to roll a CPAN dev release yet.

(Also, a fresh head tomorrow might spot some bugs or corner cases that I
missed)


Thanks for your insights on this.

Nicholas Clark

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