Blame SOURCES/perl-5.26.2-RC1-Parse-caret-vars-with-subscripts-the-same-as-normal-.patch

425bb7
From 07ebe9c4fb1028d17e61caabe8c15abd0cd48983 Mon Sep 17 00:00:00 2001
243a19
From: Yves Orton <demerphq@gmail.com>
243a19
Date: Thu, 29 Jun 2017 11:31:14 +0200
243a19
Subject: [PATCH] Parse caret vars with subscripts the same as normal vars
243a19
 inside of ${..} escaping
243a19
MIME-Version: 1.0
243a19
Content-Type: text/plain; charset=UTF-8
243a19
Content-Transfer-Encoding: 8bit
243a19
243a19
This behavior is discussed in perl #131664, which complains that
243a19
"${^CAPTURE}[0]" does not work as expected. Abigail explains the
243a19
behavior is by design and Eirik Berg Hanssen expands on that explanation
243a19
pointing out that what /should/ work, "${^CAPTURE[0]}" does not,
243a19
which Sawyer then ruled was a bug.
243a19
243a19
So this patch makes "${^CAPTURE[0]}" (and "${^CAPTURE [0]}" [hi
243a19
abigial]) work the same as they would if the var was called @foo.
243a19
425bb7
Petr Písař: Ported to 5.26.2-RC1.
425bb7
243a19
Signed-off-by: Petr Písař <ppisar@redhat.com>
243a19
---
243a19
 t/base/lex.t | 28 +++++++++++++++++++++++++++-
243a19
 toke.c       | 46 +++++++++++++++++++++++++---------------------
243a19
 2 files changed, 52 insertions(+), 22 deletions(-)
243a19
243a19
diff --git a/t/base/lex.t b/t/base/lex.t
425bb7
index 99fd3bb..ae17bbd 100644
243a19
--- a/t/base/lex.t
243a19
+++ b/t/base/lex.t
243a19
@@ -1,6 +1,6 @@
243a19
 #!./perl
243a19
 
425bb7
-print "1..112\n";
425bb7
+print "1..119\n";
243a19
 
243a19
 $x = 'x';
243a19
 
243a19
@@ -154,6 +154,32 @@ my $test = 31;
243a19
   print "not " unless index ($@, 'Can\'t use global $^XYZ in "my"') > -1;
243a19
   print "ok $test\n"; $test++;
243a19
 #  print "($@)\n" if $@;
243a19
+#
243a19
+  ${^TEST}= "splat";
243a19
+  @{^TEST}= ("foo", "bar");
243a19
+  %{^TEST}= ("foo" => "FOO", "bar" => "BAR" );
243a19
+
243a19
+  print "not " if "${^TEST}" ne "splat";
243a19
+  print "ok $test\n"; $test++;
243a19
+
243a19
+  print "not " if "${^TEST}[0]" ne "splat[0]";
243a19
+  print "ok $test\n"; $test++;
243a19
+
243a19
+  print "not " if "${^TEST[0]}" ne "foo";
243a19
+  print "ok $test\n"; $test++;
243a19
+
243a19
+  print "not " if "${ ^TEST [1] }" ne "bar";
243a19
+  print "ok $test\n"; $test++;
243a19
+
243a19
+  print "not " if "${^TEST}{foo}" ne "splat{foo}";
243a19
+  print "ok $test\n"; $test++;
243a19
+
243a19
+  print "not " if "${^TEST{foo}}" ne "FOO";
243a19
+  print "ok $test\n"; $test++;
243a19
+
243a19
+  print "not " if "${ ^TEST {bar} }" ne "BAR";
243a19
+  print "ok $test\n"; $test++;
243a19
+
243a19
 
243a19
 # Now let's make sure that caret variables are all forced into the main package.
243a19
   package Someother;
243a19
diff --git a/toke.c b/toke.c
425bb7
index ee9c464..aff785b 100644
243a19
--- a/toke.c
243a19
+++ b/toke.c
425bb7
@@ -9416,19 +9416,36 @@ S_scan_ident(pTHX_ char *s, char *dest, STRLEN destlen, I32 ck_uni)
243a19
         bool skip;
243a19
         char *s2;
243a19
         /* If we were processing {...} notation then...  */
243a19
-        if (isIDFIRST_lazy_if_safe(d, e, is_utf8)) {
243a19
-            /* if it starts as a valid identifier, assume that it is one.
243a19
-               (the later check for } being at the expected point will trap
243a19
-               cases where this doesn't pan out.)  */
243a19
-            d += is_utf8 ? UTF8SKIP(d) : 1;
243a19
-            parse_ident(&s, &d, e, 1, is_utf8, TRUE);
243a19
-	    *d = '\0';
243a19
+        if (isIDFIRST_lazy_if_safe(d, e, is_utf8)
243a19
+            || (!isPRINT(*d) /* isCNTRL(d), plus all non-ASCII */
243a19
+                 && isWORDCHAR(*s))
243a19
+        ) {
243a19
+            /* note we have to check for a normal identifier first,
243a19
+             * as it handles utf8 symbols, and only after that has
243a19
+             * been ruled out can we look at the caret words */
243a19
+            if (isIDFIRST_lazy_if_safe(d, e, is_utf8) ) {
243a19
+                /* if it starts as a valid identifier, assume that it is one.
243a19
+                   (the later check for } being at the expected point will trap
243a19
+                   cases where this doesn't pan out.)  */
243a19
+                d += is_utf8 ? UTF8SKIP(d) : 1;
243a19
+                parse_ident(&s, &d, e, 1, is_utf8, TRUE);
243a19
+                *d = '\0';
243a19
+            }
243a19
+            else { /* caret word: ${^Foo} ${^CAPTURE[0]} */
243a19
+                d++;
243a19
+                while (isWORDCHAR(*s) && d < e) {
243a19
+                    *d++ = *s++;
243a19
+                }
243a19
+                if (d >= e)
243a19
+                    Perl_croak(aTHX_ "%s", ident_too_long);
243a19
+                *d = '\0';
243a19
+            }
243a19
             tmp_copline = CopLINE(PL_curcop);
243a19
             if (s < PL_bufend && isSPACE(*s)) {
243a19
                 s = skipspace(s);
243a19
             }
243a19
 	    if ((*s == '[' || (*s == '{' && strNE(dest, "sub")))) {
243a19
-                /* ${foo[0]} and ${foo{bar}} notation.  */
243a19
+                /* ${foo[0]} and ${foo{bar}} and ${^CAPTURE[0]} notation.  */
243a19
 		if (ckWARN(WARN_AMBIGUOUS) && keyword(dest, d - dest, 0)) {
243a19
 		    const char * const brack =
243a19
 			(const char *)
425bb7
@@ -9447,19 +9464,6 @@ S_scan_ident(pTHX_ char *s, char *dest, STRLEN destlen, I32 ck_uni)
243a19
 		return s;
243a19
 	    }
243a19
 	}
243a19
-	/* Handle extended ${^Foo} variables
243a19
-	 * 1999-02-27 mjd-perl-patch@plover.com */
243a19
-	else if (! isPRINT(*d) /* isCNTRL(d), plus all non-ASCII */
243a19
-		 && isWORDCHAR(*s))
243a19
-	{
243a19
-	    d++;
243a19
-	    while (isWORDCHAR(*s) && d < e) {
243a19
-		*d++ = *s++;
243a19
-	    }
243a19
-	    if (d >= e)
243a19
-		Perl_croak(aTHX_ "%s", ident_too_long);
243a19
-	    *d = '\0';
243a19
-	}
243a19
 
243a19
         if ( !tmp_copline )
243a19
             tmp_copline = CopLINE(PL_curcop);
243a19
-- 
425bb7
2.14.3
243a19