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
Ed Avis
September 17, 2016 07:53
RE: [perl #129283] Compile-time check for $1, $2 om RHS of s///operator
Message ID:
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 <>

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 Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About