This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
RMG: fix typo, clarify instructions a bit
[perl5.git] / t / op / substr.t
old mode 100755 (executable)
new mode 100644 (file)
index fe53f01..eae2403
@@ -1,14 +1,15 @@
 #!./perl
 
-print "1..106\n";
-
-$ENV{PERL_DESTRUCT_LEVEL} = 0; # XXX known to leaks scalars
-
 #P = start of string  Q = start of substr  R = end of substr  S = end of string
 
-$a = 'abcdefxyz';
-BEGIN { $^W = 1 };
+BEGIN {
+    chdir 't' if -d 't';
+    require './test.pl';
+    set_up_inc('../lib');
+}
+use warnings ;
 
+$a = 'abcdefxyz';
 $SIG{__WARN__} = sub {
      if ($_[0] =~ /^substr outside of string/) {
           $w++;
@@ -21,139 +22,197 @@ $SIG{__WARN__} = sub {
      }
 };
 
-sub fail { !defined(shift) && $w-- };
+plan(388);
+
+run_tests() unless caller;
 
-print (substr($a,0,3) eq 'abc' ? "ok 1\n" : "not ok 1\n");   # P=Q R S
-print (substr($a,3,3) eq 'def' ? "ok 2\n" : "not ok 2\n");   # P Q R S
-print (substr($a,6,999) eq 'xyz' ? "ok 3\n" : "not ok 3\n"); # P Q S R
-print (fail(substr($a,999,999)) ? "ok 4\n" : "not ok 4\n");  # P R Q S
-print (substr($a,0,-6) eq 'abc' ? "ok 5\n" : "not ok 5\n");  # P=Q R S
-print (substr($a,-3,1) eq 'x' ? "ok 6\n" : "not ok 6\n");    # P Q R S
+my $krunch = "a";
 
-$[ = 1;
+sub run_tests {
 
-print (substr($a,1,3) eq 'abc' ? "ok 7\n" : "not ok 7\n");   # P=Q R S
-print (substr($a,4,3) eq 'def' ? "ok 8\n" : "not ok 8\n");   # P Q R S
-print (substr($a,7,999) eq 'xyz' ? "ok 9\n" : "not ok 9\n"); # P Q S R
-print (fail(substr($a,999,999)) ? "ok 10\n" : "not ok 10\n");# P R Q S
-print (substr($a,1,-6) eq 'abc' ? "ok 11\n" : "not ok 11\n");# P=Q R S
-print (substr($a,-3,1) eq 'x' ? "ok 12\n" : "not ok 12\n");  # P Q R S
+$FATAL_MSG = qr/^substr outside of string/;
 
-$[ = 0;
+is(substr($a,0,3), 'abc');   # P=Q R S
+is(substr($a,3,3), 'def');   # P Q R S
+is(substr($a,6,999), 'xyz'); # P Q S R
+$b = substr($a,999,999) ; # warn # P R Q S
+is ($w--, 1);
+eval{substr($a,999,999) = "" ; };# P R Q S
+like ($@, $FATAL_MSG);
+is(substr($a,0,-6), 'abc');  # P=Q R S
+is(substr($a,-3,1), 'x');    # P Q R S
+sub{$b = shift}->(substr($a,999,999));
+is ($w--, 1, 'boundless lvalue substr only warns on fetch');
 
 substr($a,3,3) = 'XYZ';
-print $a eq 'abcXYZxyz' ? "ok 13\n" : "not ok 13\n";
+is($a, 'abcXYZxyz' );
 substr($a,0,2) = '';
-print $a eq 'cXYZxyz' ? "ok 14\n" : "not ok 14\n";
+is($a, 'cXYZxyz' );
 substr($a,0,0) = 'ab';
-print $a eq 'abcXYZxyz' ? "ok 15\n" : "not ok 15 $a\n";
+is($a, 'abcXYZxyz' );
 substr($a,0,0) = '12345678';
-print $a eq '12345678abcXYZxyz' ? "ok 16\n" : "not ok 16\n";
+is($a, '12345678abcXYZxyz' );
 substr($a,-3,3) = 'def';
-print $a eq '12345678abcXYZdef' ? "ok 17\n" : "not ok 17\n";
+is($a, '12345678abcXYZdef');
 substr($a,-3,3) = '<';
-print $a eq '12345678abcXYZ<' ? "ok 18\n" : "not ok 18\n";
+is($a, '12345678abcXYZ<' );
 substr($a,-1,1) = '12345678';
-print $a eq '12345678abcXYZ12345678' ? "ok 19\n" : "not ok 19\n";
+is($a, '12345678abcXYZ12345678' );
 
 $a = 'abcdefxyz';
 
-print (substr($a,6) eq 'xyz' ? "ok 20\n" : "not ok 20\n");   # P Q R=S
-print (substr($a,-3) eq 'xyz' ? "ok 21\n" : "not ok 21\n");  # P Q R=S
-print (fail(substr($a,999)) ? "ok 22\n" : "not ok 22\n");    # P R=S Q
-print (substr($a,0) eq 'abcdefxyz' ? "ok 23\n" : "not ok 23\n");# P=Q R=S
-print (substr($a,9) eq '' ? "ok 24\n" : "not ok 24\n");      # P Q=R=S
-print (substr($a,-11) eq 'abcdefxyz' ? "ok 25\n" : "not ok 25\n");# Q P R=S
-print (substr($a,-9) eq 'abcdefxyz' ? "ok 26\n" : "not ok 26\n");  # P=Q R=S
+is(substr($a,6), 'xyz' );        # P Q R=S
+is(substr($a,-3), 'xyz' );       # P Q R=S
+$b = substr($a,999,999) ; # warning   # P R=S Q
+is($w--, 1);
+eval{substr($a,999,999) = "" ; } ;    # P R=S Q
+like($@, $FATAL_MSG);
+is(substr($a,0), 'abcdefxyz');  # P=Q R=S
+is(substr($a,9), '');           # P Q=R=S
+is(substr($a,-11), 'abcdefxyz'); # Q P R=S
+is(substr($a,-9), 'abcdefxyz');  # P=Q R=S
 
 $a = '54321';
 
-print (fail(substr($a,-7, 1)) ? "ok 27\n" : "not ok 27\n");  # Q R P S
-print (fail(substr($a,-7,-6)) ? "ok 28\n" : "not ok 28\n");  # Q R P S
-print (substr($a,-5,-7) eq '' ? "ok 29\n" : "not ok 29\n");  # R P=Q S
-print (substr($a, 2,-7) eq '' ? "ok 30\n" : "not ok 30\n");  # R P Q S
-print (substr($a,-3,-7) eq '' ? "ok 31\n" : "not ok 31\n");  # R P Q S
-print (substr($a, 2,-5) eq '' ? "ok 32\n" : "not ok 32\n");  # P=R Q S
-print (substr($a,-3,-5) eq '' ? "ok 33\n" : "not ok 33\n");  # P=R Q S
-print (substr($a, 2,-4) eq '' ? "ok 34\n" : "not ok 34\n");  # P R Q S
-print (substr($a,-3,-4) eq '' ? "ok 35\n" : "not ok 35\n");  # P R Q S
-print (substr($a, 5,-6) eq '' ? "ok 36\n" : "not ok 36\n");  # R P Q=S
-print (substr($a, 5,-5) eq '' ? "ok 37\n" : "not ok 37\n");  # P=R Q S
-print (substr($a, 5,-3) eq '' ? "ok 38\n" : "not ok 38\n");  # P R Q=S
-print (fail(substr($a, 7,-7)) ? "ok 39\n" : "not ok 39\n");  # R P S Q
-print (fail(substr($a, 7,-5)) ? "ok 40\n" : "not ok 40\n");  # P=R S Q
-print (fail(substr($a, 7,-3)) ? "ok 41\n" : "not ok 41\n");  # P R S Q
-print (fail(substr($a, 7, 0)) ? "ok 42\n" : "not ok 42\n");  # P S Q=R
-
-print (substr($a,-7,2) eq '' ? "ok 43\n" : "not ok 43\n");   # Q P=R S
-print (substr($a,-7,4) eq '54' ? "ok 44\n" : "not ok 44\n"); # Q P R S
-print (substr($a,-7,7) eq '54321' ? "ok 45\n" : "not ok 45\n");# Q P R=S
-print (substr($a,-7,9) eq '54321' ? "ok 46\n" : "not ok 46\n");# Q P S R
-print (substr($a,-5,0) eq '' ? "ok 47\n" : "not ok 47\n");   # P=Q=R S
-print (substr($a,-5,3) eq '543' ? "ok 48\n" : "not ok 48\n");# P=Q R S
-print (substr($a,-5,5) eq '54321' ? "ok 49\n" : "not ok 49\n");# P=Q R=S
-print (substr($a,-5,7) eq '54321' ? "ok 50\n" : "not ok 50\n");# P=Q S R
-print (substr($a,-3,0) eq '' ? "ok 51\n" : "not ok 51\n");   # P Q=R S
-print (substr($a,-3,3) eq '321' ? "ok 52\n" : "not ok 52\n");# P Q R=S
-print (substr($a,-2,3) eq '21' ? "ok 53\n" : "not ok 53\n"); # P Q S R
-print (substr($a,0,-5) eq '' ? "ok 54\n" : "not ok 54\n");   # P=Q=R S
-print (substr($a,2,-3) eq '' ? "ok 55\n" : "not ok 55\n");   # P Q=R S
-print (substr($a,0,0) eq '' ? "ok 56\n" : "not ok 56\n");    # P=Q=R S
-print (substr($a,0,5) eq '54321' ? "ok 57\n" : "not ok 57\n");# P=Q R=S
-print (substr($a,0,7) eq '54321' ? "ok 58\n" : "not ok 58\n");# P=Q S R
-print (substr($a,2,0) eq '' ? "ok 59\n" : "not ok 59\n");    # P Q=R S
-print (substr($a,2,3) eq '321' ? "ok 60\n" : "not ok 60\n"); # P Q R=S
-print (substr($a,5,0) eq '' ? "ok 61\n" : "not ok 61\n");    # P Q=R=S
-print (substr($a,5,2) eq '' ? "ok 62\n" : "not ok 62\n");    # P Q=S R
-print (substr($a,-7,-5) eq '' ? "ok 63\n" : "not ok 63\n");  # Q P=R S
-print (substr($a,-7,-2) eq '543' ? "ok 64\n" : "not ok 64\n");# Q P R S
-print (substr($a,-5,-5) eq '' ? "ok 65\n" : "not ok 65\n");  # P=Q=R S
-print (substr($a,-5,-2) eq '543' ? "ok 66\n" : "not ok 66\n");# P=Q R S
-print (substr($a,-3,-3) eq '' ? "ok 67\n" : "not ok 67\n");  # P Q=R S
-print (substr($a,-3,-1) eq '32' ? "ok 68\n" : "not ok 68\n");# P Q R S
+$b = substr($a,-7, 1) ; # warn  # Q R P S
+is($w--, 1);
+eval{substr($a,-7, 1) = "" ; }; # Q R P S
+like($@, $FATAL_MSG);
+$b = substr($a,-7,-6) ; # warn  # Q R P S
+is($w--, 1);
+eval{substr($a,-7,-6) = "" ; }; # Q R P S
+like($@, $FATAL_MSG);
+is(substr($a,-5,-7), '');  # R P=Q S
+is(substr($a, 2,-7), '');  # R P Q S
+is(substr($a,-3,-7), '');  # R P Q S
+is(substr($a, 2,-5), '');  # P=R Q S
+is(substr($a,-3,-5), '');  # P=R Q S
+is(substr($a, 2,-4), '');  # P R Q S
+is(substr($a,-3,-4), '');  # P R Q S
+is(substr($a, 5,-6), '');  # R P Q=S
+is(substr($a, 5,-5), '');  # P=R Q S
+is(substr($a, 5,-3), '');  # P R Q=S
+$b = substr($a, 7,-7) ; # warn  # R P S Q
+is($w--, 1);
+eval{substr($a, 7,-7) = "" ; }; # R P S Q
+like($@, $FATAL_MSG);
+$b = substr($a, 7,-5) ; # warn  # P=R S Q
+is($w--, 1);
+eval{substr($a, 7,-5) = "" ; }; # P=R S Q
+like($@, $FATAL_MSG);
+$b = substr($a, 7,-3) ; # warn  # P Q S Q
+is($w--, 1);
+eval{substr($a, 7,-3) = "" ; }; # P Q S Q
+like($@, $FATAL_MSG);
+$b = substr($a, 7, 0) ; # warn  # P S Q=R
+is($w--, 1);
+eval{substr($a, 7, 0) = "" ; }; # P S Q=R
+like($@, $FATAL_MSG);
+
+is(substr($a,-7,2), '');   # Q P=R S
+is(substr($a,-7,4), '54'); # Q P R S
+is(substr($a,-7,7), '54321');# Q P R=S
+is(substr($a,-7,9), '54321');# Q P S R
+is(substr($a,-5,0), '');   # P=Q=R S
+is(substr($a,-5,3), '543');# P=Q R S
+is(substr($a,-5,5), '54321');# P=Q R=S
+is(substr($a,-5,7), '54321');# P=Q S R
+is(substr($a,-3,0), '');   # P Q=R S
+is(substr($a,-3,3), '321');# P Q R=S
+is(substr($a,-2,3), '21'); # P Q S R
+is(substr($a,0,-5), '');   # P=Q=R S
+is(substr($a,2,-3), '');   # P Q=R S
+is(substr($a,0,0), '');    # P=Q=R S
+is(substr($a,0,5), '54321');# P=Q R=S
+is(substr($a,0,7), '54321');# P=Q S R
+is(substr($a,2,0), '');    # P Q=R S
+is(substr($a,2,3), '321'); # P Q R=S
+is(substr($a,5,0), '');    # P Q=R=S
+is(substr($a,5,2), '');    # P Q=S R
+is(substr($a,-7,-5), '');  # Q P=R S
+is(substr($a,-7,-2), '543');# Q P R S
+is(substr($a,-5,-5), '');  # P=Q=R S
+is(substr($a,-5,-2), '543');# P=Q R S
+is(substr($a,-3,-3), '');  # P Q=R S
+is(substr($a,-3,-1), '32');# P Q R S
 
 $a = '';
 
-print (substr($a,-2,2) eq '' ? "ok 69\n" : "not ok 69\n");   # Q P=R=S
-print (substr($a,0,0) eq '' ? "ok 70\n" : "not ok 70\n");    # P=Q=R=S
-print (substr($a,0,1) eq '' ? "ok 71\n" : "not ok 71\n");    # P=Q=S R
-print (substr($a,-2,3) eq '' ? "ok 72\n" : "not ok 72\n");   # Q P=S R
-print (substr($a,-2) eq '' ? "ok 73\n" : "not ok 73\n");     # Q P=R=S
-print (substr($a,0) eq '' ? "ok 74\n" : "not ok 74\n");      # P=Q=R=S
+is(substr($a,-2,2), '');   # Q P=R=S
+is(substr($a,0,0), '');    # P=Q=R=S
+is(substr($a,0,1), '');    # P=Q=S R
+is(substr($a,-2,3), '');   # Q P=S R
+is(substr($a,-2), '');     # Q P=R=S
+is(substr($a,0), '');      # P=Q=R=S
+
 
+is(substr($a,0,-1), '');   # R P=Q=S
+$b = substr($a,-2, 0) ; # warn  # Q=R P=S
+is($w--, 1);
+eval{substr($a,-2, 0) = "" ; }; # Q=R P=S
+like($@, $FATAL_MSG);
 
-print (substr($a,0,-1) eq '' ? "ok 75\n" : "not ok 75\n");   # R P=Q=S
-print (fail(substr($a,-2,0)) ? "ok 76\n" : "not ok 76\n");   # Q=R P=S
-print (fail(substr($a,-2,1)) ? "ok 77\n" : "not ok 77\n");   # Q R P=S
-print (fail(substr($a,-2,-1)) ? "ok 78\n" : "not ok 78\n");  # Q R P=S
-print (fail(substr($a,-2,-2)) ? "ok 79\n" : "not ok 79\n");  # Q=R P=S
-print (fail(substr($a,1,-2)) ? "ok 80\n" : "not ok 81\n");   # R P=S Q
-print (fail(substr($a,1,1)) ? "ok 81\n" : "not ok 81\n");    # P=S Q R
-print (fail(substr($a,1,0)) ? "ok 82\n" : "not ok 82\n");    # P=S Q=R
-print (fail(substr($a,1)) ? "ok 83\n" : "not ok 83\n");      # P=R=S Q
+$b = substr($a,-2, 1) ; # warn  # Q R P=S
+is($w--, 1);
+eval{substr($a,-2, 1) = "" ; }; # Q R P=S
+like($@, $FATAL_MSG);
 
+$b = substr($a,-2,-1) ; # warn  # Q R P=S
+is($w--, 1);
+eval{substr($a,-2,-1) = "" ; }; # Q R P=S
+like($@, $FATAL_MSG);
+
+$b = substr($a,-2,-2) ; # warn  # Q=R P=S
+is($w--, 1);
+eval{substr($a,-2,-2) = "" ; }; # Q=R P=S
+like($@, $FATAL_MSG);
+
+$b = substr($a, 1,-2) ; # warn  # R P=S Q
+is($w--, 1);
+eval{substr($a, 1,-2) = "" ; }; # R P=S Q
+like($@, $FATAL_MSG);
+
+$b = substr($a, 1, 1) ; # warn  # P=S Q R
+is($w--, 1);
+eval{substr($a, 1, 1) = "" ; }; # P=S Q R
+like($@, $FATAL_MSG);
+
+$b = substr($a, 1, 0) ;# warn   # P=S Q=R
+is($w--, 1);
+eval{substr($a, 1, 0) = "" ; }; # P=S Q=R
+like($@, $FATAL_MSG);
+
+$b = substr($a,1) ; # warning   # P=R=S Q
+is($w--, 1);
+eval{substr($a,1) = "" ; };     # P=R=S Q
+like($@, $FATAL_MSG);
+
+$b = substr($a,-7,-6) ; # warn  # Q R P S
+is($w--, 1);
+eval{substr($a,-7,-6) = "" ; }; # Q R P S
+like($@, $FATAL_MSG);
 
 my $a = 'zxcvbnm';
 substr($a,2,0) = '';
-print $a eq 'zxcvbnm' ? "ok 84\n" : "not ok 84\n";
+is($a, 'zxcvbnm');
 substr($a,7,0) = '';
-print $a eq 'zxcvbnm' ? "ok 85\n" : "not ok 85\n";
+is($a, 'zxcvbnm');
 substr($a,5,0) = '';
-print $a eq 'zxcvbnm' ? "ok 86\n" : "not ok 86\n";
+is($a, 'zxcvbnm');
 substr($a,0,2) = 'pq';
-print $a eq 'pqcvbnm' ? "ok 87\n" : "not ok 87\n";
+is($a, 'pqcvbnm');
 substr($a,2,0) = 'r';
-print $a eq 'pqrcvbnm' ? "ok 88\n" : "not ok 88\n";
+is($a, 'pqrcvbnm');
 substr($a,8,0) = 'asd';
-print $a eq 'pqrcvbnmasd' ? "ok 89\n" : "not ok 89\n";
+is($a, 'pqrcvbnmasd');
 substr($a,0,2) = 'iop';
-print $a eq 'ioprcvbnmasd' ? "ok 90\n" : "not ok 90\n";
+is($a, 'ioprcvbnmasd');
 substr($a,0,5) = 'fgh';
-print $a eq 'fghvbnmasd' ? "ok 91\n" : "not ok 91\n";
+is($a, 'fghvbnmasd');
 substr($a,3,5) = 'jkl';
-print $a eq 'fghjklsd' ? "ok 92\n" : "not ok 92\n";
+is($a, 'fghjklsd');
 substr($a,3,2) = '1234';
-print $a eq 'fgh1234lsd' ? "ok 93\n" : "not ok 93\n";
+is($a, 'fgh1234lsd');
 
 
 # with lexicals (and in re-entered scopes)
@@ -162,52 +221,651 @@ for (0,1) {
   unless ($_) {
     $txt = "Foo";
     substr($txt, -1) = "X";
-    print $txt eq "FoX" ? "ok 94\n" : "not ok 94\n";
+    is($txt, "FoX");
   }
   else {
-    local $^W = 0;    # because of (spurious?) "uninitialised value"
     substr($txt, 0, 1) = "X";
-    print $txt eq "X" ? "ok 95\n" : "not ok 95\n";
+    is($txt, "X");
   }
 }
 
+$w = 0 ;
 # coercion of references
 {
   my $s = [];
   substr($s, 0, 1) = 'Foo';
-  print substr($s,0,7) eq "FooRRAY" && !($w-=2) ? "ok 96\n" : "not ok 96\n";
+  is (substr($s,0,7), "FooRRAY");
+  is ($w,2);
+  $w = 0;
 }
 
 # check no spurious warnings
-print $w ? "not ok 97\n" : "ok 97\n";
+is($w, 0);
 
 # check new 4 arg replacement syntax
 $a = "abcxyz";
 $w = 0;
-print "not " unless substr($a, 0, 3, "") eq "abc" && $a eq "xyz";
-print "ok 98\n";
-print "not " unless substr($a, 0, 0, "abc") eq "" && $a eq "abcxyz";
-print "ok 99\n";
-print "not " unless substr($a, 3, -1, "") eq "xy" && $a eq "abcz";
-print "ok 100\n";
-
-print "not " unless substr($a, 3, undef, "xy") eq "" && $a eq "abcxyz"
-                 && $w == 3;
-print "ok 101\n";
+is(substr($a, 0, 3, ""), "abc");
+is($a, "xyz");
+is(substr($a, 0, 0, "abc"), "");
+is($a, "abcxyz");
+is(substr($a, 3, -1, ""), "xy");
+is($a, "abcz");
+
+is(substr($a, 3, undef, "xy"), "");
+is($a, "abcxyz");
+is($w, 3);
+
 $w = 0;
 
-print "not " unless substr($a, 3, 9999999, "") eq "xyz" && $a eq "abc";
-print "ok 102\n";
-print "not " unless fail(substr($a, -99, 0, ""));
-print "ok 103\n";
-print "not " unless fail(substr($a, 99, 3, ""));
-print "ok 104\n";
+is(substr($a, 3, 9999999, ""), "xyz");
+is($a, "abc");
+eval{substr($a, -99, 0, "") };
+like($@, $FATAL_MSG);
+eval{substr($a, 99, 3, "") };
+like($@, $FATAL_MSG);
 
 substr($a, 0, length($a), "foo");
-print "not " unless $a eq "foo" && !$w;
-print "ok 105\n";
+is ($a, "foo");
+is ($w, 0);
 
 # using 4 arg substr as lvalue is a compile time error
 eval 'substr($a,0,0,"") = "abc"';
-print "not " unless $@ && $@ =~ /Can't modify substr/ && $a eq "foo";
-print "ok 106\n";
+like ($@, qr/Can't modify substr/);
+is ($a, "foo");
+
+$a = "abcdefgh";
+is(sub { shift }->(substr($a, 0, 4, "xxxx")), 'abcd');
+is($a, 'xxxxefgh');
+
+{
+    my $y = 10;
+    $y = "2" . $y;
+    is ($y, 210);
+}
+
+# utf8 sanity
+{
+    my $x = substr("a\x{263a}b",0);
+    is(length($x), 3);
+    $x = substr($x,1,1);
+    is($x, "\x{263a}");
+    $x = $x x 2;
+    is(length($x), 2);
+    substr($x,0,1) = "abcd";
+    is($x, "abcd\x{263a}");
+    is(length($x), 5);
+    $x = reverse $x;
+    is(length($x), 5);
+    is($x, "\x{263a}dcba");
+
+    my $z = 10;
+    $z = "21\x{263a}" . $z;
+    is(length($z), 5);
+    is($z, "21\x{263a}10");
+}
+
+# replacement should work on magical values
+require Tie::Scalar;
+my %data;
+tie $data{'a'}, 'Tie::StdScalar';  # makes $data{'a'} magical
+$data{a} = "firstlast";
+is(substr($data{'a'}, 0, 5, ""), "first");
+is($data{'a'}, "last");
+
+# more utf8
+
+# The following two originally from Ignasi Roca.
+
+$x = "\xF1\xF2\xF3";
+substr($x, 0, 1) = "\x{100}"; # Ignasi had \x{FF}
+is(length($x), 3);
+is($x, "\x{100}\xF2\xF3");
+is(substr($x, 0, 1), "\x{100}");
+is(substr($x, 1, 1), "\x{F2}");
+is(substr($x, 2, 1), "\x{F3}");
+
+$x = "\xF1\xF2\xF3";
+substr($x, 0, 1) = "\x{100}\x{FF}"; # Ignasi had \x{FF}
+is(length($x), 4);
+is($x, "\x{100}\x{FF}\xF2\xF3");
+is(substr($x, 0, 1), "\x{100}");
+is(substr($x, 1, 1), "\x{FF}");
+is(substr($x, 2, 1), "\x{F2}");
+is(substr($x, 3, 1), "\x{F3}");
+
+# more utf8 lval exercise
+
+$x = "\xF1\xF2\xF3";
+substr($x, 0, 2) = "\x{100}\xFF";
+is(length($x), 3);
+is($x, "\x{100}\xFF\xF3");
+is(substr($x, 0, 1), "\x{100}");
+is(substr($x, 1, 1), "\x{FF}");
+is(substr($x, 2, 1), "\x{F3}");
+
+$x = "\xF1\xF2\xF3";
+substr($x, 1, 1) = "\x{100}\xFF";
+is(length($x), 4);
+is($x, "\xF1\x{100}\xFF\xF3");
+is(substr($x, 0, 1), "\x{F1}");
+is(substr($x, 1, 1), "\x{100}");
+is(substr($x, 2, 1), "\x{FF}");
+is(substr($x, 3, 1), "\x{F3}");
+
+$x = "\xF1\xF2\xF3";
+substr($x, 2, 1) = "\x{100}\xFF";
+is(length($x), 4);
+is($x, "\xF1\xF2\x{100}\xFF");
+is(substr($x, 0, 1), "\x{F1}");
+is(substr($x, 1, 1), "\x{F2}");
+is(substr($x, 2, 1), "\x{100}");
+is(substr($x, 3, 1), "\x{FF}");
+
+$x = "\xF1\xF2\xF3";
+substr($x, 3, 1) = "\x{100}\xFF";
+is(length($x), 5);
+is($x, "\xF1\xF2\xF3\x{100}\xFF");
+is(substr($x, 0, 1), "\x{F1}");
+is(substr($x, 1, 1), "\x{F2}");
+is(substr($x, 2, 1), "\x{F3}");
+is(substr($x, 3, 1), "\x{100}");
+is(substr($x, 4, 1), "\x{FF}");
+
+$x = "\xF1\xF2\xF3";
+substr($x, -1, 1) = "\x{100}\xFF";
+is(length($x), 4);
+is($x, "\xF1\xF2\x{100}\xFF");
+is(substr($x, 0, 1), "\x{F1}");
+is(substr($x, 1, 1), "\x{F2}");
+is(substr($x, 2, 1), "\x{100}");
+is(substr($x, 3, 1), "\x{FF}");
+
+$x = "\xF1\xF2\xF3";
+substr($x, -1, 0) = "\x{100}\xFF";
+is(length($x), 5);
+is($x, "\xF1\xF2\x{100}\xFF\xF3");
+is(substr($x, 0, 1), "\x{F1}");
+is(substr($x, 1, 1), "\x{F2}");
+is(substr($x, 2, 1), "\x{100}");
+is(substr($x, 3, 1), "\x{FF}");
+is(substr($x, 4, 1), "\x{F3}");
+
+$x = "\xF1\xF2\xF3";
+substr($x, 0, -1) = "\x{100}\xFF";
+is(length($x), 3);
+is($x, "\x{100}\xFF\xF3");
+is(substr($x, 0, 1), "\x{100}");
+is(substr($x, 1, 1), "\x{FF}");
+is(substr($x, 2, 1), "\x{F3}");
+
+$x = "\xF1\xF2\xF3";
+substr($x, 0, -2) = "\x{100}\xFF";
+is(length($x), 4);
+is($x, "\x{100}\xFF\xF2\xF3");
+is(substr($x, 0, 1), "\x{100}");
+is(substr($x, 1, 1), "\x{FF}");
+is(substr($x, 2, 1), "\x{F2}");
+is(substr($x, 3, 1), "\x{F3}");
+
+$x = "\xF1\xF2\xF3";
+substr($x, 0, -3) = "\x{100}\xFF";
+is(length($x), 5);
+is($x, "\x{100}\xFF\xF1\xF2\xF3");
+is(substr($x, 0, 1), "\x{100}");
+is(substr($x, 1, 1), "\x{FF}");
+is(substr($x, 2, 1), "\x{F1}");
+is(substr($x, 3, 1), "\x{F2}");
+is(substr($x, 4, 1), "\x{F3}");
+
+$x = "\xF1\xF2\xF3";
+substr($x, 1, -1) = "\x{100}\xFF";
+is(length($x), 4);
+is($x, "\xF1\x{100}\xFF\xF3");
+is(substr($x, 0, 1), "\x{F1}");
+is(substr($x, 1, 1), "\x{100}");
+is(substr($x, 2, 1), "\x{FF}");
+is(substr($x, 3, 1), "\x{F3}");
+
+$x = "\xF1\xF2\xF3";
+substr($x, -1, -1) = "\x{100}\xFF";
+is(length($x), 5);
+is($x, "\xF1\xF2\x{100}\xFF\xF3");
+is(substr($x, 0, 1), "\x{F1}");
+is(substr($x, 1, 1), "\x{F2}");
+is(substr($x, 2, 1), "\x{100}");
+is(substr($x, 3, 1), "\x{FF}");
+is(substr($x, 4, 1), "\x{F3}");
+
+# And tests for already-UTF8 one
+
+$x = "\x{101}\x{F2}\x{F3}";
+substr($x, 0, 1) = "\x{100}";
+is(length($x), 3);
+is($x, "\x{100}\xF2\xF3");
+is(substr($x, 0, 1), "\x{100}");
+is(substr($x, 1, 1), "\x{F2}");
+is(substr($x, 2, 1), "\x{F3}");
+
+$x = "\x{101}\x{F2}\x{F3}";
+substr($x, 0, 1) = "\x{100}\x{FF}";
+is(length($x), 4);
+is($x, "\x{100}\x{FF}\xF2\xF3");
+is(substr($x, 0, 1), "\x{100}");
+is(substr($x, 1, 1), "\x{FF}");
+is(substr($x, 2, 1), "\x{F2}");
+is(substr($x, 3, 1), "\x{F3}");
+
+$x = "\x{101}\x{F2}\x{F3}";
+substr($x, 0, 2) = "\x{100}\xFF";
+is(length($x), 3);
+is($x, "\x{100}\xFF\xF3");
+is(substr($x, 0, 1), "\x{100}");
+is(substr($x, 1, 1), "\x{FF}");
+is(substr($x, 2, 1), "\x{F3}");
+
+$x = "\x{101}\x{F2}\x{F3}";
+substr($x, 1, 1) = "\x{100}\xFF";
+is(length($x), 4);
+is($x, "\x{101}\x{100}\xFF\xF3");
+is(substr($x, 0, 1), "\x{101}");
+is(substr($x, 1, 1), "\x{100}");
+is(substr($x, 2, 1), "\x{FF}");
+is(substr($x, 3, 1), "\x{F3}");
+
+$x = "\x{101}\x{F2}\x{F3}";
+substr($x, 2, 1) = "\x{100}\xFF";
+is(length($x), 4);
+is($x, "\x{101}\xF2\x{100}\xFF");
+is(substr($x, 0, 1), "\x{101}");
+is(substr($x, 1, 1), "\x{F2}");
+is(substr($x, 2, 1), "\x{100}");
+is(substr($x, 3, 1), "\x{FF}");
+
+$x = "\x{101}\x{F2}\x{F3}";
+substr($x, 3, 1) = "\x{100}\xFF";
+is(length($x), 5);
+is($x, "\x{101}\x{F2}\x{F3}\x{100}\xFF");
+is(substr($x, 0, 1), "\x{101}");
+is(substr($x, 1, 1), "\x{F2}");
+is(substr($x, 2, 1), "\x{F3}");
+is(substr($x, 3, 1), "\x{100}");
+is(substr($x, 4, 1), "\x{FF}");
+
+$x = "\x{101}\x{F2}\x{F3}";
+substr($x, -1, 1) = "\x{100}\xFF";
+is(length($x), 4);
+is($x, "\x{101}\xF2\x{100}\xFF");
+is(substr($x, 0, 1), "\x{101}");
+is(substr($x, 1, 1), "\x{F2}");
+is(substr($x, 2, 1), "\x{100}");
+is(substr($x, 3, 1), "\x{FF}");
+
+$x = "\x{101}\x{F2}\x{F3}";
+substr($x, -1, 0) = "\x{100}\xFF";
+is(length($x), 5);
+is($x, "\x{101}\xF2\x{100}\xFF\xF3");
+is(substr($x, 0, 1), "\x{101}");
+is(substr($x, 1, 1), "\x{F2}");
+is(substr($x, 2, 1), "\x{100}");
+is(substr($x, 3, 1), "\x{FF}");
+is(substr($x, 4, 1), "\x{F3}");
+
+$x = "\x{101}\x{F2}\x{F3}";
+substr($x, 0, -1) = "\x{100}\xFF";
+is(length($x), 3);
+is($x, "\x{100}\xFF\xF3");
+is(substr($x, 0, 1), "\x{100}");
+is(substr($x, 1, 1), "\x{FF}");
+is(substr($x, 2, 1), "\x{F3}");
+
+$x = "\x{101}\x{F2}\x{F3}";
+substr($x, 0, -2) = "\x{100}\xFF";
+is(length($x), 4);
+is($x, "\x{100}\xFF\xF2\xF3");
+is(substr($x, 0, 1), "\x{100}");
+is(substr($x, 1, 1), "\x{FF}");
+is(substr($x, 2, 1), "\x{F2}");
+is(substr($x, 3, 1), "\x{F3}");
+
+$x = "\x{101}\x{F2}\x{F3}";
+substr($x, 0, -3) = "\x{100}\xFF";
+is(length($x), 5);
+is($x, "\x{100}\xFF\x{101}\x{F2}\x{F3}");
+is(substr($x, 0, 1), "\x{100}");
+is(substr($x, 1, 1), "\x{FF}");
+is(substr($x, 2, 1), "\x{101}");
+is(substr($x, 3, 1), "\x{F2}");
+is(substr($x, 4, 1), "\x{F3}");
+
+$x = "\x{101}\x{F2}\x{F3}";
+substr($x, 1, -1) = "\x{100}\xFF";
+is(length($x), 4);
+is($x, "\x{101}\x{100}\xFF\xF3");
+is(substr($x, 0, 1), "\x{101}");
+is(substr($x, 1, 1), "\x{100}");
+is(substr($x, 2, 1), "\x{FF}");
+is(substr($x, 3, 1), "\x{F3}");
+
+$x = "\x{101}\x{F2}\x{F3}";
+substr($x, -1, -1) = "\x{100}\xFF";
+is(length($x), 5);
+is($x, "\x{101}\xF2\x{100}\xFF\xF3");
+is(substr($x, 0, 1), "\x{101}");
+is(substr($x, 1, 1), "\x{F2}");
+is(substr($x, 2, 1), "\x{100}");
+is(substr($x, 3, 1), "\x{FF}");
+is(substr($x, 4, 1), "\x{F3}");
+
+substr($x = "ab", 0, 0, "\x{100}\x{200}");
+is($x, "\x{100}\x{200}ab");
+
+substr($x = "\x{100}\x{200}", 0, 0, "ab");
+is($x, "ab\x{100}\x{200}");
+
+substr($x = "ab", 1, 0, "\x{100}\x{200}");
+is($x, "a\x{100}\x{200}b");
+
+substr($x = "\x{100}\x{200}", 1, 0, "ab");
+is($x, "\x{100}ab\x{200}");
+
+substr($x = "ab", 2, 0, "\x{100}\x{200}");
+is($x, "ab\x{100}\x{200}");
+
+substr($x = "\x{100}\x{200}", 2, 0, "ab");
+is($x, "\x{100}\x{200}ab");
+
+substr($x = "\xFFb", 0, 0, "\x{100}\x{200}");
+is($x, "\x{100}\x{200}\xFFb");
+
+substr($x = "\x{100}\x{200}", 0, 0, "\xFFb");
+is($x, "\xFFb\x{100}\x{200}");
+
+substr($x = "\xFFb", 1, 0, "\x{100}\x{200}");
+is($x, "\xFF\x{100}\x{200}b");
+
+substr($x = "\x{100}\x{200}", 1, 0, "\xFFb");
+is($x, "\x{100}\xFFb\x{200}");
+
+substr($x = "\xFFb", 2, 0, "\x{100}\x{200}");
+is($x, "\xFFb\x{100}\x{200}");
+
+substr($x = "\x{100}\x{200}", 2, 0, "\xFFb");
+is($x, "\x{100}\x{200}\xFFb");
+
+# [perl #20933]
+{ 
+    my $s = "ab";
+    my @r; 
+    $r[$_] = \ substr $s, $_, 1 for (0, 1);
+    is(join("", map { $$_ } @r), "ab");
+}
+
+# [perl #23207]
+{
+    sub ss {
+       substr($_[0],0,1) ^= substr($_[0],1,1) ^=
+       substr($_[0],0,1) ^= substr($_[0],1,1);
+    }
+    my $x = my $y = 'AB'; ss $x; ss $y;
+    is($x, $y);
+}
+
+# [perl #24605]
+{
+    my $x = "0123456789\x{500}";
+    my $y = substr $x, 4;
+    is(substr($x, 7, 1), "7");
+}
+
+# multiple assignments to lvalue [perl #24346]   
+{
+    my $x = "abcdef";
+    for (substr($x,1,3)) {
+       is($_, 'bcd');
+       $_ = 'XX';
+       is($_, 'XX');
+       is($x, 'aXXef'); 
+       $_ = "\xFF";
+       is($_, "\xFF"); 
+       is($x, "a\xFFef");
+       $_ = "\xF1\xF2\xF3\xF4\xF5\xF6";
+       is($_, "\xF1\xF2\xF3\xF4\xF5\xF6");
+       is($x, "a\xF1\xF2\xF3\xF4\xF5\xF6ef"); 
+       $_ = 'YYYY';
+       is($_, 'YYYY'); 
+       is($x, 'aYYYYef');
+    }
+    $x = "abcdef";
+    for (substr($x,1)) {
+       is($_, 'bcdef');
+       $_ = 'XX';
+       is($_, 'XX');
+       is($x, 'aXX');
+       $x .= "frompswiggle";
+       is $_, "XXfrompswiggle";
+    }
+    $x = "abcdef";
+    for (substr($x,1,-1)) {
+       is($_, 'bcde');
+       $_ = 'XX';
+       is($_, 'XX');
+       is($x, 'aXXf');
+       $x .= "frompswiggle";
+       is $_, "XXffrompswiggl";
+    }
+    $x = "abcdef";
+    for (substr($x,-5,3)) {
+       is($_, 'bcd');
+       $_ = 'XX';   # now $_ is substr($x, -4, 2)
+       is($_, 'XX');
+       is($x, 'aXXef');
+       $x .= "frompswiggle";
+       is $_, "gg";
+    }
+    $x = "abcdef";
+    for (substr($x,-5)) {
+       is($_, 'bcdef');
+       $_ = 'XX';  # now substr($x, -2)
+       is($_, 'XX');
+       is($x, 'aXX');
+       $x .= "frompswiggle";
+       is $_, "le";
+    }
+    $x = "abcdef";
+    for (substr($x,-5,-1)) {
+       is($_, 'bcde');
+       $_ = 'XX';  # now substr($x, -3, -1)
+       is($_, 'XX');
+       is($x, 'aXXf');
+       $x .= "frompswiggle";
+       is $_, "gl";
+    }
+}
+
+# Also part of perl #24346; scalar(substr...) should not affect lvalueness
+{
+    my $str = "abcdef";
+    sub { $_[0] = 'dea' }->( scalar substr $str, 3, 2 );
+    is $str, 'abcdeaf', 'scalar does not affect lvalueness of substr';
+}
+
+# [perl #24200] string corruption with lvalue sub
+
+{
+    sub bar: lvalue { substr $krunch, 0 }
+    bar = "XXX";
+    is(bar, 'XXX');
+    $krunch = '123456789';
+    is(bar, '123456789');
+}
+
+# [perl #29149]
+{
+    my $text  = "0123456789\xED ";
+    utf8::upgrade($text);
+    my $pos = 5;
+    pos($text) = $pos;
+    my $a = substr($text, $pos, $pos);
+    is(substr($text,$pos,1), $pos);
+
+}
+
+# [perl #23765]
+{
+    my $a = pack("C", 0xbf);
+    no warnings 'deprecated';
+    substr($a, -1) &= chr(0xfeff);
+    is($a, "\xbf");
+}
+
+# [perl #34976] incorrect caching of utf8 substr length
+{
+    my  $a = "abcd\x{100}";
+    is(substr($a,1,2), 'bc');
+    is(substr($a,1,1), 'b');
+}
+
+# [perl #62646] offsets exceeding 32 bits on 64-bit system
+SKIP: {
+    skip("32-bit system", 24) unless ~0 > 0xffffffff;
+    my $a = "abc";
+    my $s;
+    my $r;
+
+    utf8::downgrade($a);
+    for (1..2) {
+       $w = 0;
+       $r = substr($a, 0xffffffff, 1);
+       is($r, undef);
+       is($w, 1);
+
+       $w = 0;
+       $r = substr($a, 0xffffffff+1, 1);
+       is($r, undef);
+       is($w, 1);
+
+       $w = 0;
+       ok( !eval { $r = substr($s=$a, 0xffffffff, 1, "_"); 1 } );
+       is($r, undef);
+       is($s, $a);
+       is($w, 0);
+
+       $w = 0;
+       ok( !eval { $r = substr($s=$a, 0xffffffff+1, 1, "_"); 1 } );
+       is($r, undef);
+       is($s, $a);
+       is($w, 0);
+
+       utf8::upgrade($a);
+    }
+}
+
+# [perl #77692] UTF8 cache not being reset when TARG is reused
+ok eval {
+ local ${^UTF8CACHE} = -1;
+ for my $i (0..1)
+ {
+   my $dummy = length(substr("\x{100}",0,$i));
+ }
+ 1
+}, 'UTF8 cache is reset when TARG is reused [perl #77692]';
+
+{
+    use utf8;
+    use open qw( :utf8 :std );
+    no warnings 'once';
+
+    my $t = "";
+    substr $t, 0, 0, *ワルド;
+    is($t, "*main::ワルド", "substr works on UTF-8 globs");
+
+    $t = "The World!";
+    substr $t, 0, 9, *ザ::ワルド;
+    is($t, "*ザ::ワルド!", "substr works on a UTF-8 glob + stash");
+}
+
+{
+    my $x = *foo;
+    my $y = \substr *foo, 0, 0;
+    is ref \$x, 'GLOB', '\substr does not coerce its glob arg just yet';
+    $x = \"foo";
+    $y = \substr *foo, 0, 0;
+    is ref \$x, 'REF', '\substr does not coerce its ref arg just yet';
+}
+
+# Test that UTF8-ness of magic var changing does not confuse substr lvalue
+# assignment.
+# We use overloading for our magic var, but a typeglob would work, too.
+package o {
+    use overload '""' => sub { ++our $count; $_[0][0] }
+}
+my $refee = bless ["\x{100}a"], o::;
+my $substr = \substr $refee, -2;       # UTF8 flag still off for $$substr.
+$$substr = "b";                                # UTF8 flag turns on when setsubstr
+is $refee, "b",                                # magic stringifies $$substr.
+     'substr lvalue assignment when stringification turns on UTF8ness';
+
+# Test that changing UTF8-ness does not confuse 4-arg substr.
+$refee = bless [], "\x{100}a";
+# stringify without returning on UTF8 flag on $refee:
+my $string = $refee; $string = "$string";
+substr $refee, 0, 0, "\xff";
+is $refee, "\xff$string",
+  '4-arg substr with target UTF8ness turning on when stringified';
+$refee = bless [], "\x{100}";
+() = "$refee"; # UTF8 flag now on
+bless $refee, "\xff";
+$string = $refee; $string = "$string";
+substr $refee, 0, 0, "\xff";
+is $refee, "\xff$string",
+  '4-arg substr with target UTF8ness turning off when stringified';
+
+# Overload count
+$refee = bless ["foo"], o::;
+$o::count = 0;
+substr $refee, 0, 0, "";
+is $o::count, 1, '4-arg substr calls overloading once on the target';
+$refee = bless ["\x{100}"], o::;
+() = "$refee"; # turn UTF8 flag on
+$o::count = 0;
+() = substr $refee, 0;
+is $o::count, 1, 'rvalue substr calls overloading once on utf8 target';
+$o::count = 0;
+$refee = "";
+${\substr $refee, 0} = bless ["\x{100}"], o::;
+is $o::count, 1, 'assigning utf8 overload to substr lvalue calls ovld 1ce';
+
+# [perl #7678] core dump with substr reference and localisation
+{$b="abcde"; local $k; *k=\substr($b, 2, 1);}
+
+} # sub run_tests - put tests above this line that can run in threads
+
+
+my $destroyed;
+{ package Class; DESTROY { ++$destroyed; } }
+
+$destroyed = 0;
+{
+    my $x = '';
+    substr($x,0,1) = "";
+    $x = bless({}, 'Class');
+}
+is($destroyed, 1, 'Timely scalar destruction with lvalue substr');
+
+{
+    my $result_3363;
+    sub a_3363 {
+        my ($word, $replace) = @_;
+        my $ref = \substr($word, 0, 1);
+        $$ref = $replace;
+        if ($replace eq "b") {
+            $result_3363 = $word;
+        } else {
+            a_3363($word, "b");
+        }
+    }
+    a_3363($_, "v") for "test";
+
+    is($result_3363, "best", "ref-to-substr retains lvalue-ness under recursion [perl #3363]");
+}