develooper Front page | perl.perl5.porters | Postings from September 2016

RE: [perl #129283] Compile-time check for $1, $2 om RHS of s///operator

Thread Previous
From:
demerphq
Date:
September 17, 2016 15:07
Subject:
RE: [perl #129283] Compile-time check for $1, $2 om RHS of s///operator
Message ID:
CANgJU+WfJFo5p3-RwSXfqp9Cu+bdznHwpOU=KHE0ToOitXKAyQ@mail.gmail.com
On 17 Sep 2016 3:54 a.m., "Ed Avis" <eda@waniasset.com> wrote:
>
> Lukas M, Thanks for your reply.  It is true that the $2 variable can
always be read, even if no regexp match has happened so far.

I am less convinced by this line of reasoning than others.

>I guess the point is that the capture buffer number 2 does not exist.
Note that sed, for example, does give a hard error if you try to refer to a
capturing group that doesn't exist:
>
> % sed -E 's/(a)/\1\2/'
> sed: -e expression #1, char 11: invalid reference \2 on `s' command's RHS
>
> (at least for GNU sed).  But I don't want to get bogged down in the
theological question of whether $2 in the RHS can be said to 'exist',
whether such a check would be an extension of 'use strict', or indeed
whether it should be a warning or an error.

But I think that is precisely the discussion we should have.

Currently we cannot distinguish $2 is undef because it did not match and $2
is undef because there is no capture buffer declared for it in the pattern.

In theory we can distinguish these cases and throw an additional warning.

But we can't do this at compile time, as we cannot tell in advance of regex
execution whether the user will actually use a missing var.  Consider

s/$qr/$1 ? $3 : $2 /e

We have no way to know if we will inappropriately access a missing $3 until
we actually do so.

> Simple substitutions such as s/a(b)c/$1/ are a commonly used part of
Perl, and it would be great to give the programmer more help in avoiding
mistakes in them, as sed and other tools do.

Agreed.  It would actually be trivial to make us generate an additional
warning when this happens.

Yves

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About