diff options
-rw-r--r-- | pod/perldiag.pod | 14 | ||||
-rw-r--r-- | t/lib/warnings/toke | 15 | ||||
-rw-r--r-- | toke.c | 7 |
3 files changed, 35 insertions, 1 deletions
diff --git a/pod/perldiag.pod b/pod/perldiag.pod index c5125598bf..775e274ffc 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<m/$\/> in a regex. +The regex C<m/foo$\s+bar/m> translates to: match the word 'foo', the output +record separartor (see L<perlvar/$\>) 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<m/${\}/> (for example: C<m/foo${\}s+bar/>). + +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<m/$(?)\/> (for example: C<m/foo$(?)\s+bar/>). + =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 1bd8f8fa9c..04c41d5cf5 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. @@ -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 */ |