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

Re: qr'$foo' and unescaped $ signs

Thread Previous | Thread Next
Nicholas Clark
June 30, 2021 10:41
Re: qr'$foo' and unescaped $ signs
Message ID:
On Tue, Jun 29, 2021 at 02:30:01PM +0000, Nicholas Clark wrote:
> One of the obscure features of regular expressions is that if one uses a
> single quote as the delimiter, no interpolation takes place.
> One result of this - there's a Data::Dumper bug about handling them:
> You get this:
> $ perl -MData::Dumper -w
> print Dumper(qr'$foo');
> __END__
> $VAR1 = qr/(?^:$foo)/;

> meaning that if one scans the internal string of the regex (stored as the PV)
> and finds an unescaped $ anywhere (other than the last character) then

or $) or $|

These two in qr// don't interpolate the magic variables.
I think that all the others do.

This doesn't seem to be documented anywhere

> 1) the *only* way that could have got there was by being written as qr''
> 2) the *only* way to convert that back to a regex is to output qr''
>    (ie there exists no escaping syntax capable of recreating it inside qr//)

> I suspect that this rabbit hole goes deeper. What did I miss? What did I get
> wrong?

I missed the new bit above.

Also, I can't find any way to make a regex with an embedded $ somewhere in
the middle that actually matches any string. Even with qr''m so that the $
will match a "\n" in the middle of a string.

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.

basically, is there any string that qr'$foo'm will match?

Am I right?
And is *that* documented anywhere?

Nicholas Clark

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About