Change 34224 by [EMAIL PROTECTED] on 2008/08/24 14:52:12

        Subject: [PATCH] Unintented interpolation of $/ in regex (was: Re: [perl
        From: Bram <[EMAIL PROTECTED]>
        Date: Thu, 24 Jul 2008 18:14:27 +0200
        Message-ID: <[EMAIL PROTECTED]>
        
        This adds a new warning.
        I moved the tests from the original patch to t/lib/warnings/toke.

Affected files ...

... //depot/perl/pod/perldiag.pod#498 edit
... //depot/perl/t/lib/warnings/toke#23 edit
... //depot/perl/toke.c#827 edit

Differences ...

==== //depot/perl/pod/perldiag.pod#498 (text) ====
Index: perl/pod/perldiag.pod
--- perl/pod/perldiag.pod#497~34138~    2008-07-13 13:22:25.000000000 -0700
+++ perl/pod/perldiag.pod       2008-08-24 07:52:12.000000000 -0700
@@ -3419,6 +3419,20 @@
 literal @foo, then write it as [EMAIL PROTECTED]; 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:

==== //depot/perl/t/lib/warnings/toke#23 (text) ====
Index: perl/t/lib/warnings/toke
--- perl/t/lib/warnings/toke#22~34023~  2008-06-08 02:12:01.000000000 -0700
+++ perl/t/lib/warnings/toke    2008-08-24 07:52:12.000000000 -0700
@@ -875,3 +875,18 @@
 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.

==== //depot/perl/toke.c#827 (text) ====
Index: perl/toke.c
--- perl/toke.c#826~34071~      2008-06-17 09:50:57.000000000 -0700
+++ perl/toke.c 2008-08-24 07:52:12.000000000 -0700
@@ -2173,8 +2173,13 @@
        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 */
End of Patch.

Reply via email to