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 | Thread Next
From:
Ed Avis
Date:
September 17, 2016 07:53
Subject:
RE: [perl #129283] Compile-time check for $1, $2 om RHS of s///operator
Message ID:
ebcd3438831240888abc85e8f073139e@WCL-EX13MBX.wcl.local
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 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.

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.

-- 
Ed Avis <eda@waniasset.com>

This email is intended only for the person to whom it is addressed and may contain confidential information. Any retransmission, copying, disclosure or other use of, this information by persons other than the intended recipient is prohibited. If you received this email in error, please contact the sender and delete the material. This email is for information only and is not intended as an offer or solicitation for the purchase or sale of any financial instrument. Wadhwani Asset Management LLP is a Limited Liability Partnership registered in England (OC303168) with registered office at 9th Floor Orion House, 5 Upper St Martin’s Lane, London, WC2H 9EA. It is authorised and regulated by the Financial Conduct Authority.

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