From: Bram Date: Thu, 24 Jul 2008 18:14:27 +0000 (+0200) Subject: Unintented interpolation of $/ in regex (was: Re: [perl X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=777723442a58c6c86c224d8d746a30441a5bdfbd;p=p5sagit%2Fp5-mst-13.2.git Unintented interpolation of $/ in regex (was: Re: [perl Message-ID: <20080724181427.aiml4sdvr40k4coc@horde.wizbit.be> Note that the Subject: has a typo - it should be $\ This adds a new warning. I moved the tests from the original patch to t/lib/warnings/toke. p4raw-id: //depot/perl@34224 --- diff --git a/pod/perldiag.pod b/pod/perldiag.pod index c512559..775e274 100644 --- a/pod/perldiag.pod +++ b/pod/perldiag.pod @@ -3419,6 +3419,20 @@ but there was no array C<@foo> in scope at the time. If you wanted a literal @foo, then write it as \@foo; otherwise find out what happened to the array you apparently lost track of. +=item Possible unintended interpolation of $\ in regex + +(W ambiguous) You said something like C in a regex. +The regex C translates to: match the word 'foo', the output +record separartor (see L) and the letter 's' (one time or more) +followed by the word 'bar'. + +If this is what you intended then you can silence the warning by using +C (for example: C). + +If instead you intended to match the word 'foo' at the end of the line +followed by whitespace and the word 'bar' on the next line then you can use +C (for example: C). + =item pragma "attrs" is deprecated, use "sub NAME : ATTRS" instead (D deprecated) You have written something like this: diff --git a/t/lib/warnings/toke b/t/lib/warnings/toke index 1bd8f8f..04c41d5 100644 --- a/t/lib/warnings/toke +++ b/t/lib/warnings/toke @@ -875,3 +875,18 @@ Prototype after '@' for main::proto_after_array : @$ at - line 3. Prototype after '%' for main::proto_after_hash : %$ at - line 7. Illegal character after '_' in prototype for main::underscore_fail : $_$ at - line 12. Prototype after '@' for main::underscore_after_at : @_ at - line 13. +######## +# toke.c +use warnings "ambiguous"; +"foo\nn" =~ /^foo$\n/; +"foo\nn" =~ /^foo${\}n/; +my $foo = qr/^foo$\n/; +my $bar = qr/^foo${\}n/; +no warnings "ambiguous"; +"foo\nn" =~ /^foo$\n/; +"foo\nn" =~ /^foo${\}n/; +my $foo = qr/^foo$\n/; +my $bar = qr/^foo${\}n/; +EXPECT +Possible unintended interpolation of $\ in regex at - line 3. +Possible unintended interpolation of $\ in regex at - line 5. diff --git a/toke.c b/toke.c index e98e47c..b2ba362 100644 --- a/toke.c +++ b/toke.c @@ -2173,8 +2173,13 @@ S_scan_const(pTHX_ char *start) else if (*s == '$') { if (!PL_lex_inpat) /* not a regexp, so $ must be var */ break; - if (s + 1 < send && !strchr("()| \r\n\t", s[1])) + if (s + 1 < send && !strchr("()| \r\n\t", s[1])) { + if (s[1] == '\\' && ckWARN(WARN_AMBIGUOUS)) { + Perl_warner(aTHX_ packWARN(WARN_AMBIGUOUS), + "Possible unintended interpolation of $\\ in regex"); + } break; /* in regexp, $ might be tail anchor */ + } } /* End of else if chain - OP_TRANS rejoin rest */