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

Re: qr'$foo' and unescaped $ signs

Thread Previous | Thread Next
From:
Karl Williamson
Date:
June 30, 2021 12:18
Subject:
Re: qr'$foo' and unescaped $ signs
Message ID:
2a387a98-4879-3b71-1825-79258c6b7ba8@khwilliamson.com
On 6/30/21 4:41 AM, Nicholas Clark wrote:
> 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:
>>
>> https://rt.cpan.org/Public/Bug/Display.html?id=84569
>>
>> 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
> 

$ blead -Dr -le "qr'$foo'm"
Compiling REx ""
Final program:
    1: NOTHING (2)
    2: END (0)
minlen 0
Enabling $` $& $' support (0x7).

 From regcomp.sym:
NOTHING     NOTHING,    no        ; Match empty string.

$ blead -Dr -le "qr'\$foo'm"
Compiling REx "$foo"
Final program:
    1: MEOL (2)
    2: EXACT <foo> (4)
    4: END (0)
anchored "foo" at 0..0 (checking anchored) minlen 3

regcomp.sym:
SEOL        EOL,        no        ; Match "" at end of line: /$/
MEOL        EOL,        no        ; Same, assuming multiline: /$/m

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