3 #P = start of string Q = start of substr R = end of substr S = end of string
12 $SIG{__WARN__} = sub {
13 if ($_[0] =~ /^substr outside of string/) {
15 } elsif ($_[0] =~ /^Attempt to use reference as lvalue in substr/) {
17 } elsif ($_[0] =~ /^Use of uninitialized value/) {
28 $FATAL_MSG = qr/^substr outside of string/;
30 is(substr($a,0,3), 'abc'); # P=Q R S
31 is(substr($a,3,3), 'def'); # P Q R S
32 is(substr($a,6,999), 'xyz'); # P Q S R
33 $b = substr($a,999,999) ; # warn # P R Q S
35 eval{substr($a,999,999) = "" ; };# P R Q S
36 like ($@, $FATAL_MSG);
37 is(substr($a,0,-6), 'abc'); # P=Q R S
38 is(substr($a,-3,1), 'x'); # P Q R S
42 is(substr($a,1,3), 'abc' ); # P=Q R S
43 is(substr($a,4,3), 'def' ); # P Q R S
44 is(substr($a,7,999), 'xyz');# P Q S R
45 $b = substr($a,999,999) ; # warn # P R Q S
47 eval{substr($a,999,999) = "" ; } ; # P R Q S
48 like ($@, $FATAL_MSG);
49 is(substr($a,1,-6), 'abc' );# P=Q R S
50 is(substr($a,-3,1), 'x' ); # P Q R S
54 substr($a,3,3) = 'XYZ';
58 substr($a,0,0) = 'ab';
60 substr($a,0,0) = '12345678';
61 is($a, '12345678abcXYZxyz' );
62 substr($a,-3,3) = 'def';
63 is($a, '12345678abcXYZdef');
64 substr($a,-3,3) = '<';
65 is($a, '12345678abcXYZ<' );
66 substr($a,-1,1) = '12345678';
67 is($a, '12345678abcXYZ12345678' );
71 is(substr($a,6), 'xyz' ); # P Q R=S
72 is(substr($a,-3), 'xyz' ); # P Q R=S
73 $b = substr($a,999,999) ; # warning # P R=S Q
75 eval{substr($a,999,999) = "" ; } ; # P R=S Q
77 is(substr($a,0), 'abcdefxyz'); # P=Q R=S
78 is(substr($a,9), ''); # P Q=R=S
79 is(substr($a,-11), 'abcdefxyz'); # Q P R=S
80 is(substr($a,-9), 'abcdefxyz'); # P=Q R=S
84 $b = substr($a,-7, 1) ; # warn # Q R P S
86 eval{substr($a,-7, 1) = "" ; }; # Q R P S
88 $b = substr($a,-7,-6) ; # warn # Q R P S
90 eval{substr($a,-7,-6) = "" ; }; # Q R P S
92 is(substr($a,-5,-7), ''); # R P=Q S
93 is(substr($a, 2,-7), ''); # R P Q S
94 is(substr($a,-3,-7), ''); # R P Q S
95 is(substr($a, 2,-5), ''); # P=R Q S
96 is(substr($a,-3,-5), ''); # P=R Q S
97 is(substr($a, 2,-4), ''); # P R Q S
98 is(substr($a,-3,-4), ''); # P R Q S
99 is(substr($a, 5,-6), ''); # R P Q=S
100 is(substr($a, 5,-5), ''); # P=R Q S
101 is(substr($a, 5,-3), ''); # P R Q=S
102 $b = substr($a, 7,-7) ; # warn # R P S Q
104 eval{substr($a, 7,-7) = "" ; }; # R P S Q
105 like($@, $FATAL_MSG);
106 $b = substr($a, 7,-5) ; # warn # P=R S Q
108 eval{substr($a, 7,-5) = "" ; }; # P=R S Q
109 like($@, $FATAL_MSG);
110 $b = substr($a, 7,-3) ; # warn # P Q S Q
112 eval{substr($a, 7,-3) = "" ; }; # P Q S Q
113 like($@, $FATAL_MSG);
114 $b = substr($a, 7, 0) ; # warn # P S Q=R
116 eval{substr($a, 7, 0) = "" ; }; # P S Q=R
117 like($@, $FATAL_MSG);
119 is(substr($a,-7,2), ''); # Q P=R S
120 is(substr($a,-7,4), '54'); # Q P R S
121 is(substr($a,-7,7), '54321');# Q P R=S
122 is(substr($a,-7,9), '54321');# Q P S R
123 is(substr($a,-5,0), ''); # P=Q=R S
124 is(substr($a,-5,3), '543');# P=Q R S
125 is(substr($a,-5,5), '54321');# P=Q R=S
126 is(substr($a,-5,7), '54321');# P=Q S R
127 is(substr($a,-3,0), ''); # P Q=R S
128 is(substr($a,-3,3), '321');# P Q R=S
129 is(substr($a,-2,3), '21'); # P Q S R
130 is(substr($a,0,-5), ''); # P=Q=R S
131 is(substr($a,2,-3), ''); # P Q=R S
132 is(substr($a,0,0), ''); # P=Q=R S
133 is(substr($a,0,5), '54321');# P=Q R=S
134 is(substr($a,0,7), '54321');# P=Q S R
135 is(substr($a,2,0), ''); # P Q=R S
136 is(substr($a,2,3), '321'); # P Q R=S
137 is(substr($a,5,0), ''); # P Q=R=S
138 is(substr($a,5,2), ''); # P Q=S R
139 is(substr($a,-7,-5), ''); # Q P=R S
140 is(substr($a,-7,-2), '543');# Q P R S
141 is(substr($a,-5,-5), ''); # P=Q=R S
142 is(substr($a,-5,-2), '543');# P=Q R S
143 is(substr($a,-3,-3), ''); # P Q=R S
144 is(substr($a,-3,-1), '32');# P Q R S
148 is(substr($a,-2,2), ''); # Q P=R=S
149 is(substr($a,0,0), ''); # P=Q=R=S
150 is(substr($a,0,1), ''); # P=Q=S R
151 is(substr($a,-2,3), ''); # Q P=S R
152 is(substr($a,-2), ''); # Q P=R=S
153 is(substr($a,0), ''); # P=Q=R=S
156 is(substr($a,0,-1), ''); # R P=Q=S
157 $b = substr($a,-2, 0) ; # warn # Q=R P=S
159 eval{substr($a,-2, 0) = "" ; }; # Q=R P=S
160 like($@, $FATAL_MSG);
162 $b = substr($a,-2, 1) ; # warn # Q R P=S
164 eval{substr($a,-2, 1) = "" ; }; # Q R P=S
165 like($@, $FATAL_MSG);
167 $b = substr($a,-2,-1) ; # warn # Q R P=S
169 eval{substr($a,-2,-1) = "" ; }; # Q R P=S
170 like($@, $FATAL_MSG);
172 $b = substr($a,-2,-2) ; # warn # Q=R P=S
174 eval{substr($a,-2,-2) = "" ; }; # Q=R P=S
175 like($@, $FATAL_MSG);
177 $b = substr($a, 1,-2) ; # warn # R P=S Q
179 eval{substr($a, 1,-2) = "" ; }; # R P=S Q
180 like($@, $FATAL_MSG);
182 $b = substr($a, 1, 1) ; # warn # P=S Q R
184 eval{substr($a, 1, 1) = "" ; }; # P=S Q R
185 like($@, $FATAL_MSG);
187 $b = substr($a, 1, 0) ;# warn # P=S Q=R
189 eval{substr($a, 1, 0) = "" ; }; # P=S Q=R
190 like($@, $FATAL_MSG);
192 $b = substr($a,1) ; # warning # P=R=S Q
194 eval{substr($a,1) = "" ; }; # P=R=S Q
195 like($@, $FATAL_MSG);
204 substr($a,0,2) = 'pq';
206 substr($a,2,0) = 'r';
208 substr($a,8,0) = 'asd';
209 is($a, 'pqrcvbnmasd');
210 substr($a,0,2) = 'iop';
211 is($a, 'ioprcvbnmasd');
212 substr($a,0,5) = 'fgh';
213 is($a, 'fghvbnmasd');
214 substr($a,3,5) = 'jkl';
216 substr($a,3,2) = '1234';
217 is($a, 'fgh1234lsd');
220 # with lexicals (and in re-entered scopes)
225 substr($txt, -1) = "X";
229 substr($txt, 0, 1) = "X";
235 # coercion of references
238 substr($s, 0, 1) = 'Foo';
239 is (substr($s,0,7), "FooRRAY");
244 # check no spurious warnings
247 # check new 4 arg replacement syntax
250 is(substr($a, 0, 3, ""), "abc");
252 is(substr($a, 0, 0, "abc"), "");
254 is(substr($a, 3, -1, ""), "xy");
257 is(substr($a, 3, undef, "xy"), "");
263 is(substr($a, 3, 9999999, ""), "xyz");
265 eval{substr($a, -99, 0, "") };
266 like($@, $FATAL_MSG);
267 eval{substr($a, 99, 3, "") };
268 like($@, $FATAL_MSG);
270 substr($a, 0, length($a), "foo");
274 # using 4 arg substr as lvalue is a compile time error
275 eval 'substr($a,0,0,"") = "abc"';
276 like ($@, qr/Can't modify substr/);
280 is(sub { shift }->(substr($a, 0, 4, "xxxx")), 'abcd');
291 my $x = substr("a\x{263a}b",0);
297 substr($x,0,1) = "abcd";
298 is($x, "abcd\x{263a}");
302 is($x, "\x{263a}dcba");
305 $z = "21\x{263a}" . $z;
307 is($z, "21\x{263a}10");
310 # replacement should work on magical values
313 tie $data{'a'}, 'Tie::StdScalar'; # makes $data{'a'} magical
314 $data{a} = "firstlast";
315 is(substr($data{'a'}, 0, 5, ""), "first");
316 is($data{'a'}, "last");
320 # The following two originally from Ignasi Roca.
323 substr($x, 0, 1) = "\x{100}"; # Ignasi had \x{FF}
325 is($x, "\x{100}\xF2\xF3");
326 is(substr($x, 0, 1), "\x{100}");
327 is(substr($x, 1, 1), "\x{F2}");
328 is(substr($x, 2, 1), "\x{F3}");
331 substr($x, 0, 1) = "\x{100}\x{FF}"; # Ignasi had \x{FF}
333 is($x, "\x{100}\x{FF}\xF2\xF3");
334 is(substr($x, 0, 1), "\x{100}");
335 is(substr($x, 1, 1), "\x{FF}");
336 is(substr($x, 2, 1), "\x{F2}");
337 is(substr($x, 3, 1), "\x{F3}");
339 # more utf8 lval exercise
342 substr($x, 0, 2) = "\x{100}\xFF";
344 is($x, "\x{100}\xFF\xF3");
345 is(substr($x, 0, 1), "\x{100}");
346 is(substr($x, 1, 1), "\x{FF}");
347 is(substr($x, 2, 1), "\x{F3}");
350 substr($x, 1, 1) = "\x{100}\xFF";
352 is($x, "\xF1\x{100}\xFF\xF3");
353 is(substr($x, 0, 1), "\x{F1}");
354 is(substr($x, 1, 1), "\x{100}");
355 is(substr($x, 2, 1), "\x{FF}");
356 is(substr($x, 3, 1), "\x{F3}");
359 substr($x, 2, 1) = "\x{100}\xFF";
361 is($x, "\xF1\xF2\x{100}\xFF");
362 is(substr($x, 0, 1), "\x{F1}");
363 is(substr($x, 1, 1), "\x{F2}");
364 is(substr($x, 2, 1), "\x{100}");
365 is(substr($x, 3, 1), "\x{FF}");
368 substr($x, 3, 1) = "\x{100}\xFF";
370 is($x, "\xF1\xF2\xF3\x{100}\xFF");
371 is(substr($x, 0, 1), "\x{F1}");
372 is(substr($x, 1, 1), "\x{F2}");
373 is(substr($x, 2, 1), "\x{F3}");
374 is(substr($x, 3, 1), "\x{100}");
375 is(substr($x, 4, 1), "\x{FF}");
378 substr($x, -1, 1) = "\x{100}\xFF";
380 is($x, "\xF1\xF2\x{100}\xFF");
381 is(substr($x, 0, 1), "\x{F1}");
382 is(substr($x, 1, 1), "\x{F2}");
383 is(substr($x, 2, 1), "\x{100}");
384 is(substr($x, 3, 1), "\x{FF}");
387 substr($x, -1, 0) = "\x{100}\xFF";
389 is($x, "\xF1\xF2\x{100}\xFF\xF3");
390 is(substr($x, 0, 1), "\x{F1}");
391 is(substr($x, 1, 1), "\x{F2}");
392 is(substr($x, 2, 1), "\x{100}");
393 is(substr($x, 3, 1), "\x{FF}");
394 is(substr($x, 4, 1), "\x{F3}");
397 substr($x, 0, -1) = "\x{100}\xFF";
399 is($x, "\x{100}\xFF\xF3");
400 is(substr($x, 0, 1), "\x{100}");
401 is(substr($x, 1, 1), "\x{FF}");
402 is(substr($x, 2, 1), "\x{F3}");
405 substr($x, 0, -2) = "\x{100}\xFF";
407 is($x, "\x{100}\xFF\xF2\xF3");
408 is(substr($x, 0, 1), "\x{100}");
409 is(substr($x, 1, 1), "\x{FF}");
410 is(substr($x, 2, 1), "\x{F2}");
411 is(substr($x, 3, 1), "\x{F3}");
414 substr($x, 0, -3) = "\x{100}\xFF";
416 is($x, "\x{100}\xFF\xF1\xF2\xF3");
417 is(substr($x, 0, 1), "\x{100}");
418 is(substr($x, 1, 1), "\x{FF}");
419 is(substr($x, 2, 1), "\x{F1}");
420 is(substr($x, 3, 1), "\x{F2}");
421 is(substr($x, 4, 1), "\x{F3}");
424 substr($x, 1, -1) = "\x{100}\xFF";
426 is($x, "\xF1\x{100}\xFF\xF3");
427 is(substr($x, 0, 1), "\x{F1}");
428 is(substr($x, 1, 1), "\x{100}");
429 is(substr($x, 2, 1), "\x{FF}");
430 is(substr($x, 3, 1), "\x{F3}");
433 substr($x, -1, -1) = "\x{100}\xFF";
435 is($x, "\xF1\xF2\x{100}\xFF\xF3");
436 is(substr($x, 0, 1), "\x{F1}");
437 is(substr($x, 1, 1), "\x{F2}");
438 is(substr($x, 2, 1), "\x{100}");
439 is(substr($x, 3, 1), "\x{FF}");
440 is(substr($x, 4, 1), "\x{F3}");
442 # And tests for already-UTF8 one
444 $x = "\x{101}\x{F2}\x{F3}";
445 substr($x, 0, 1) = "\x{100}";
447 is($x, "\x{100}\xF2\xF3");
448 is(substr($x, 0, 1), "\x{100}");
449 is(substr($x, 1, 1), "\x{F2}");
450 is(substr($x, 2, 1), "\x{F3}");
452 $x = "\x{101}\x{F2}\x{F3}";
453 substr($x, 0, 1) = "\x{100}\x{FF}";
455 is($x, "\x{100}\x{FF}\xF2\xF3");
456 is(substr($x, 0, 1), "\x{100}");
457 is(substr($x, 1, 1), "\x{FF}");
458 is(substr($x, 2, 1), "\x{F2}");
459 is(substr($x, 3, 1), "\x{F3}");
461 $x = "\x{101}\x{F2}\x{F3}";
462 substr($x, 0, 2) = "\x{100}\xFF";
464 is($x, "\x{100}\xFF\xF3");
465 is(substr($x, 0, 1), "\x{100}");
466 is(substr($x, 1, 1), "\x{FF}");
467 is(substr($x, 2, 1), "\x{F3}");
469 $x = "\x{101}\x{F2}\x{F3}";
470 substr($x, 1, 1) = "\x{100}\xFF";
472 is($x, "\x{101}\x{100}\xFF\xF3");
473 is(substr($x, 0, 1), "\x{101}");
474 is(substr($x, 1, 1), "\x{100}");
475 is(substr($x, 2, 1), "\x{FF}");
476 is(substr($x, 3, 1), "\x{F3}");
478 $x = "\x{101}\x{F2}\x{F3}";
479 substr($x, 2, 1) = "\x{100}\xFF";
481 is($x, "\x{101}\xF2\x{100}\xFF");
482 is(substr($x, 0, 1), "\x{101}");
483 is(substr($x, 1, 1), "\x{F2}");
484 is(substr($x, 2, 1), "\x{100}");
485 is(substr($x, 3, 1), "\x{FF}");
487 $x = "\x{101}\x{F2}\x{F3}";
488 substr($x, 3, 1) = "\x{100}\xFF";
490 is($x, "\x{101}\x{F2}\x{F3}\x{100}\xFF");
491 is(substr($x, 0, 1), "\x{101}");
492 is(substr($x, 1, 1), "\x{F2}");
493 is(substr($x, 2, 1), "\x{F3}");
494 is(substr($x, 3, 1), "\x{100}");
495 is(substr($x, 4, 1), "\x{FF}");
497 $x = "\x{101}\x{F2}\x{F3}";
498 substr($x, -1, 1) = "\x{100}\xFF";
500 is($x, "\x{101}\xF2\x{100}\xFF");
501 is(substr($x, 0, 1), "\x{101}");
502 is(substr($x, 1, 1), "\x{F2}");
503 is(substr($x, 2, 1), "\x{100}");
504 is(substr($x, 3, 1), "\x{FF}");
506 $x = "\x{101}\x{F2}\x{F3}";
507 substr($x, -1, 0) = "\x{100}\xFF";
509 is($x, "\x{101}\xF2\x{100}\xFF\xF3");
510 is(substr($x, 0, 1), "\x{101}");
511 is(substr($x, 1, 1), "\x{F2}");
512 is(substr($x, 2, 1), "\x{100}");
513 is(substr($x, 3, 1), "\x{FF}");
514 is(substr($x, 4, 1), "\x{F3}");
516 $x = "\x{101}\x{F2}\x{F3}";
517 substr($x, 0, -1) = "\x{100}\xFF";
519 is($x, "\x{100}\xFF\xF3");
520 is(substr($x, 0, 1), "\x{100}");
521 is(substr($x, 1, 1), "\x{FF}");
522 is(substr($x, 2, 1), "\x{F3}");
524 $x = "\x{101}\x{F2}\x{F3}";
525 substr($x, 0, -2) = "\x{100}\xFF";
527 is($x, "\x{100}\xFF\xF2\xF3");
528 is(substr($x, 0, 1), "\x{100}");
529 is(substr($x, 1, 1), "\x{FF}");
530 is(substr($x, 2, 1), "\x{F2}");
531 is(substr($x, 3, 1), "\x{F3}");
533 $x = "\x{101}\x{F2}\x{F3}";
534 substr($x, 0, -3) = "\x{100}\xFF";
536 is($x, "\x{100}\xFF\x{101}\x{F2}\x{F3}");
537 is(substr($x, 0, 1), "\x{100}");
538 is(substr($x, 1, 1), "\x{FF}");
539 is(substr($x, 2, 1), "\x{101}");
540 is(substr($x, 3, 1), "\x{F2}");
541 is(substr($x, 4, 1), "\x{F3}");
543 $x = "\x{101}\x{F2}\x{F3}";
544 substr($x, 1, -1) = "\x{100}\xFF";
546 is($x, "\x{101}\x{100}\xFF\xF3");
547 is(substr($x, 0, 1), "\x{101}");
548 is(substr($x, 1, 1), "\x{100}");
549 is(substr($x, 2, 1), "\x{FF}");
550 is(substr($x, 3, 1), "\x{F3}");
552 $x = "\x{101}\x{F2}\x{F3}";
553 substr($x, -1, -1) = "\x{100}\xFF";
555 is($x, "\x{101}\xF2\x{100}\xFF\xF3");
556 is(substr($x, 0, 1), "\x{101}");
557 is(substr($x, 1, 1), "\x{F2}");
558 is(substr($x, 2, 1), "\x{100}");
559 is(substr($x, 3, 1), "\x{FF}");
560 is(substr($x, 4, 1), "\x{F3}");
562 substr($x = "ab", 0, 0, "\x{100}\x{200}");
563 is($x, "\x{100}\x{200}ab");
565 substr($x = "\x{100}\x{200}", 0, 0, "ab");
566 is($x, "ab\x{100}\x{200}");
568 substr($x = "ab", 1, 0, "\x{100}\x{200}");
569 is($x, "a\x{100}\x{200}b");
571 substr($x = "\x{100}\x{200}", 1, 0, "ab");
572 is($x, "\x{100}ab\x{200}");
574 substr($x = "ab", 2, 0, "\x{100}\x{200}");
575 is($x, "ab\x{100}\x{200}");
577 substr($x = "\x{100}\x{200}", 2, 0, "ab");
578 is($x, "\x{100}\x{200}ab");
580 substr($x = "\xFFb", 0, 0, "\x{100}\x{200}");
581 is($x, "\x{100}\x{200}\xFFb");
583 substr($x = "\x{100}\x{200}", 0, 0, "\xFFb");
584 is($x, "\xFFb\x{100}\x{200}");
586 substr($x = "\xFFb", 1, 0, "\x{100}\x{200}");
587 is($x, "\xFF\x{100}\x{200}b");
589 substr($x = "\x{100}\x{200}", 1, 0, "\xFFb");
590 is($x, "\x{100}\xFFb\x{200}");
592 substr($x = "\xFFb", 2, 0, "\x{100}\x{200}");
593 is($x, "\xFFb\x{100}\x{200}");
595 substr($x = "\x{100}\x{200}", 2, 0, "\xFFb");
596 is($x, "\x{100}\x{200}\xFFb");
602 $r[$_] = \ substr $s, $_, 1 for (0, 1);
603 is(join("", map { $$_ } @r), "ab");
609 substr($_[0],0,1) ^= substr($_[0],1,1) ^=
610 substr($_[0],0,1) ^= substr($_[0],1,1);
612 my $x = my $y = 'AB'; ss $x; ss $y;
618 my $x = "0123456789\x{500}";
619 my $y = substr $x, 4;
620 is(substr($x, 7, 1), "7");
623 # multiple assignments to lvalue [perl #24346]
626 for (substr($x,1,3)) {
634 $_ = "\xF1\xF2\xF3\xF4\xF5\xF6";
635 is($_, "\xF1\xF2\xF3\xF4\xF5\xF6");
636 is($x, "a\xF1\xF2\xF3\xF4\xF5\xF6ef");
643 # [perl #24200] string corruption with lvalue sub
647 sub bar: lvalue { substr $foo, 0 }
651 is(bar, '123456789');
656 my $text = "0123456789\xED ";
657 utf8::upgrade($text);
660 my $a = substr($text, $pos, $pos);
661 is(substr($text,$pos,1), $pos);
667 my $a = pack("C", 0xbf);
668 substr($a, -1) &= chr(0xfeff);
672 # [perl #34976] incorrect caching of utf8 substr length
674 my $a = "abcd\x{100}";
675 is(substr($a,1,2), 'bc');
676 is(substr($a,1,1), 'b');