1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
|
#!./perl -w
BEGIN {
chdir 't' if -d 't';
@INC = '../lib';
require Config; import Config;
}
require './test.pl';
plan( tests => 170 );
# Stolen from re/ReTest.pl. Can't just use the file since it doesn't support
# like() and it conflicts with test.pl
sub must_warn {
my ($code, $pattern, $name) = @_;
my $w;
local $SIG {__WARN__} = sub {$w .= join "" => @_};
use warnings 'all';
ref $code ? &$code : eval $code;
my $r = $w && $w =~ /$pattern/;
$w //= "UNDEF";
ok( $r, $name // "Got warning /$pattern/", $r ? undef :
"# expected: /$pattern/\n" .
"# result: $w" );
}
$_ = 'david';
$a = s/david/rules/r;
ok( $_ eq 'david' && $a eq 'rules', 'non-destructive substitute' );
$a = "david" =~ s/david/rules/r;
ok( $a eq 'rules', 's///r with constant' );
$a = "david" =~ s/david/"is"."great"/er;
ok( $a eq 'isgreat', 's///er' );
$a = "daviddavid" =~ s/david/cool/gr;
ok( $a eq 'coolcool', 's///gr' );
$a = 'david';
$b = $a =~ s/david/sucks/r =~ s/sucks/rules/r;
ok( $a eq 'david' && $b eq 'rules', 'chained s///r' );
$a = 'david';
$b = $a =~ s/xxx/sucks/r;
ok( $a eq 'david' && $b eq 'david', 'non matching s///r' );
$a = 'david';
for (0..2) {
ok( 'david' =~ s/$a/rules/ro eq 'rules', 's///ro '.$_ );
}
$a = 'david';
eval '$b = $a !~ s/david/is great/r';
like( $@, qr{Using !~ with s///r doesn't make sense}, 's///r !~ operator gives error' );
{
no warnings 'uninitialized';
$a = undef;
$b = $a =~ s/left/right/r;
ok ( !defined $a && !defined $b, 's///r with undef input' );
use warnings;
must_warn sub { $b = $a =~ s/left/right/r }, '^Use of uninitialized value', 's///r Uninitialized warning';
$a = 'david';
must_warn 's/david/sucks/r; 1', '^Useless use of Non-destructive substitution', 's///r void context warning';
}
$a = '';
$b = $a =~ s/david/rules/r;
ok( $a eq '' && $b eq '', 's///r on empty string' );
$_ = 'david';
@b = s/david/rules/r;
ok( $_ eq 'david' && $b[0] eq 'rules', 's///r in list context' );
# Magic value and s///r
require Tie::Scalar;
tie $m, 'Tie::StdScalar'; # makes $a magical
$m = "david";
$b = $m =~ s/david/rules/r;
ok( $m eq 'david' && $b eq 'rules', 's///r with magic input' );
$m = $b =~ s/rules/david/r;
ok( defined tied($m), 's///r magic isn\'t lost' );
$b = $m =~ s/xxx/yyy/r;
ok( ! defined tied($b), 's///r magic isn\'t contagious' );
$x = 'foo';
$_ = "x";
s/x/\$x/;
ok( $_ eq '$x', ":$_: eq :\$x:" );
$_ = "x";
s/x/$x/;
ok( $_ eq 'foo', ":$_: eq :foo:" );
$_ = "x";
s/x/\$x $x/;
ok( $_ eq '$x foo', ":$_: eq :\$x foo:" );
$b = 'cd';
($a = 'abcdef') =~ s<(b${b}e)>'\n$1';
ok( $1 eq 'bcde' && $a eq 'a\n$1f', ":$1: eq :bcde: ; :$a: eq :a\\n\$1f:" );
$a = 'abacada';
ok( ($a =~ s/a/x/g) == 4 && $a eq 'xbxcxdx' );
ok( ($a =~ s/a/y/g) == 0 && $a eq 'xbxcxdx' );
ok( ($a =~ s/b/y/g) == 1 && $a eq 'xyxcxdx' );
$_ = 'ABACADA';
ok( /a/i && s///gi && $_ eq 'BCD' );
$_ = '\\' x 4;
ok( length($_) == 4 );
$snum = s/\\/\\\\/g;
ok( $_ eq '\\' x 8 && $snum == 4 );
$_ = '\/' x 4;
ok( length($_) == 8 );
$snum = s/\//\/\//g;
ok( $_ eq '\\//' x 4 && $snum == 4 );
ok( length($_) == 12 );
$_ = 'aaaXXXXbbb';
s/^a//;
ok( $_ eq 'aaXXXXbbb' );
$_ = 'aaaXXXXbbb';
s/a//;
ok( $_ eq 'aaXXXXbbb' );
$_ = 'aaaXXXXbbb';
s/^a/b/;
ok( $_ eq 'baaXXXXbbb' );
$_ = 'aaaXXXXbbb';
s/a/b/;
ok( $_ eq 'baaXXXXbbb' );
$_ = 'aaaXXXXbbb';
s/aa//;
ok( $_ eq 'aXXXXbbb' );
$_ = 'aaaXXXXbbb';
s/aa/b/;
ok( $_ eq 'baXXXXbbb' );
$_ = 'aaaXXXXbbb';
s/b$//;
ok( $_ eq 'aaaXXXXbb' );
$_ = 'aaaXXXXbbb';
s/b//;
ok( $_ eq 'aaaXXXXbb' );
$_ = 'aaaXXXXbbb';
s/bb//;
ok( $_ eq 'aaaXXXXb' );
$_ = 'aaaXXXXbbb';
s/aX/y/;
ok( $_ eq 'aayXXXbbb' );
$_ = 'aaaXXXXbbb';
s/Xb/z/;
ok( $_ eq 'aaaXXXzbb' );
$_ = 'aaaXXXXbbb';
s/aaX.*Xbb//;
ok( $_ eq 'ab' );
$_ = 'aaaXXXXbbb';
s/bb/x/;
ok( $_ eq 'aaaXXXXxb' );
# now for some unoptimized versions of the same.
$_ = 'aaaXXXXbbb';
$x ne $x || s/^a//;
ok( $_ eq 'aaXXXXbbb' );
$_ = 'aaaXXXXbbb';
$x ne $x || s/a//;
ok( $_ eq 'aaXXXXbbb' );
$_ = 'aaaXXXXbbb';
$x ne $x || s/^a/b/;
ok( $_ eq 'baaXXXXbbb' );
$_ = 'aaaXXXXbbb';
$x ne $x || s/a/b/;
ok( $_ eq 'baaXXXXbbb' );
$_ = 'aaaXXXXbbb';
$x ne $x || s/aa//;
ok( $_ eq 'aXXXXbbb' );
$_ = 'aaaXXXXbbb';
$x ne $x || s/aa/b/;
ok( $_ eq 'baXXXXbbb' );
$_ = 'aaaXXXXbbb';
$x ne $x || s/b$//;
ok( $_ eq 'aaaXXXXbb' );
$_ = 'aaaXXXXbbb';
$x ne $x || s/b//;
ok( $_ eq 'aaaXXXXbb' );
$_ = 'aaaXXXXbbb';
$x ne $x || s/bb//;
ok( $_ eq 'aaaXXXXb' );
$_ = 'aaaXXXXbbb';
$x ne $x || s/aX/y/;
ok( $_ eq 'aayXXXbbb' );
$_ = 'aaaXXXXbbb';
$x ne $x || s/Xb/z/;
ok( $_ eq 'aaaXXXzbb' );
$_ = 'aaaXXXXbbb';
$x ne $x || s/aaX.*Xbb//;
ok( $_ eq 'ab' );
$_ = 'aaaXXXXbbb';
$x ne $x || s/bb/x/;
ok( $_ eq 'aaaXXXXxb' );
$_ = 'abc123xyz';
s/(\d+)/$1*2/e; # yields 'abc246xyz'
ok( $_ eq 'abc246xyz' );
s/(\d+)/sprintf("%5d",$1)/e; # yields 'abc 246xyz'
ok( $_ eq 'abc 246xyz' );
s/(\w)/$1 x 2/eg; # yields 'aabbcc 224466xxyyzz'
ok( $_ eq 'aabbcc 224466xxyyzz' );
$_ = "aaaaa";
ok( y/a/b/ == 5 );
ok( y/a/b/ == 0 );
ok( y/b// == 5 );
ok( y/b/c/s == 5 );
ok( y/c// == 1 );
ok( y/c//d == 1 );
ok( $_ eq "" );
$_ = "Now is the %#*! time for all good men...";
ok( ($x=(y/a-zA-Z //cd)) == 7 );
ok( y/ / /s == 8 );
$_ = 'abcdefghijklmnopqrstuvwxyz0123456789';
tr/a-z/A-Z/;
ok( $_ eq 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' );
# same as tr/A-Z/a-z/;
if (defined $Config{ebcdic} && $Config{ebcdic} eq 'define') { # EBCDIC.
no utf8;
y[\301-\351][\201-\251];
} else { # Ye Olde ASCII. Or something like it.
y[\101-\132][\141-\172];
}
ok( $_ eq 'abcdefghijklmnopqrstuvwxyz0123456789' );
SKIP: {
skip("not ASCII",1) unless (ord("+") == ord(",") - 1
&& ord(",") == ord("-") - 1
&& ord("a") == ord("b") - 1
&& ord("b") == ord("c") - 1);
$_ = '+,-';
tr/+--/a-c/;
ok( $_ eq 'abc' );
}
$_ = '+,-';
tr/+\--/a\/c/;
ok( $_ eq 'a,/' );
$_ = '+,-';
tr/-+,/ab\-/;
ok( $_ eq 'b-a' );
# test recursive substitutions
# code based on the recursive expansion of makefile variables
my %MK = (
AAAAA => '$(B)', B=>'$(C)', C => 'D', # long->short
E => '$(F)', F=>'p $(G) q', G => 'HHHHH', # short->long
DIR => '$(UNDEFINEDNAME)/xxx',
);
sub var {
my($var,$level) = @_;
return "\$($var)" unless exists $MK{$var};
return exp_vars($MK{$var}, $level+1); # can recurse
}
sub exp_vars {
my($str,$level) = @_;
$str =~ s/\$\((\w+)\)/var($1, $level+1)/ge; # can recurse
#warn "exp_vars $level = '$str'\n";
$str;
}
ok( exp_vars('$(AAAAA)',0) eq 'D' );
ok( exp_vars('$(E)',0) eq 'p HHHHH q' );
ok( exp_vars('$(DIR)',0) eq '$(UNDEFINEDNAME)/xxx' );
ok( exp_vars('foo $(DIR)/yyy bar',0) eq 'foo $(UNDEFINEDNAME)/xxx/yyy bar' );
$_ = "abcd";
s/(..)/$x = $1, m#.#/eg;
ok( $x eq "cd", 'a match nested in the RHS of a substitution' );
# Subst and lookbehind
$_="ccccc";
$snum = s/(?<!x)c/x/g;
ok( $_ eq "xxxxx" && $snum == 5 );
$_="ccccc";
$snum = s/(?<!x)(c)/x/g;
ok( $_ eq "xxxxx" && $snum == 5 );
$_="foobbarfoobbar";
$snum = s/(?<!r)foobbar/foobar/g;
ok( $_ eq "foobarfoobbar" && $snum == 1 );
$_="foobbarfoobbar";
$snum = s/(?<!ar)(foobbar)/foobar/g;
ok( $_ eq "foobarfoobbar" && $snum == 1 );
$_="foobbarfoobbar";
$snum = s/(?<!ar)foobbar/foobar/g;
ok( $_ eq "foobarfoobbar" && $snum == 1 );
eval 's{foo} # this is a comment, not a delimiter
{bar};';
ok( ! @?, 'parsing of split subst with comment' );
$snum = eval '$_="exactly"; s sxsys;m 3(yactl)3;$1';
is( $snum, 'yactl', 'alpha delimiters are allowed' );
$_="baacbaa";
$snum = tr/a/b/s;
ok( $_ eq "bbcbb" && $snum == 4,
'check if squashing works at the end of string' );
$_ = "ab";
ok( s/a/b/ == 1 );
$_ = <<'EOL';
$url = new URI::URL "http://www/"; die if $url eq "xXx";
EOL
$^R = 'junk';
$foo = ' $@%#lowercase $@%# lowercase UPPERCASE$@%#UPPERCASE' .
' $@%#lowercase$@%#lowercase$@%# lowercase lowercase $@%#lowercase' .
' lowercase $@%#MiXeD$@%# ';
$snum =
s{ \d+ \b [,.;]? (?{ 'digits' })
|
[a-z]+ \b [,.;]? (?{ 'lowercase' })
|
[A-Z]+ \b [,.;]? (?{ 'UPPERCASE' })
|
[A-Z] [a-z]+ \b [,.;]? (?{ 'Capitalized' })
|
[A-Za-z]+ \b [,.;]? (?{ 'MiXeD' })
|
[A-Za-z0-9]+ \b [,.;]? (?{ 'alphanumeric' })
|
\s+ (?{ ' ' })
|
[^A-Za-z0-9\s]+ (?{ '$@%#' })
}{$^R}xg;
ok( $_ eq $foo );
ok( $snum == 31 );
$_ = 'a' x 6;
$snum = s/a(?{})//g;
ok( $_ eq '' && $snum == 6 );
$_ = 'x' x 20;
$snum = s/(\d*|x)/<$1>/g;
$foo = '<>' . ('<x><>' x 20) ;
ok( $_ eq $foo && $snum == 41 );
$t = 'aaaaaaaaa';
$_ = $t;
pos = 6;
$snum = s/\Ga/xx/g;
ok( $_ eq 'aaaaaaxxxxxx' && $snum == 3 );
$_ = $t;
pos = 6;
$snum = s/\Ga/x/g;
ok( $_ eq 'aaaaaaxxx' && $snum == 3 );
$_ = $t;
pos = 6;
s/\Ga/xx/;
ok( $_ eq 'aaaaaaxxaa' );
$_ = $t;
pos = 6;
s/\Ga/x/;
ok( $_ eq 'aaaaaaxaa' );
$_ = $t;
$snum = s/\Ga/xx/g;
ok( $_ eq 'xxxxxxxxxxxxxxxxxx' && $snum == 9 );
$_ = $t;
$snum = s/\Ga/x/g;
ok( $_ eq 'xxxxxxxxx' && $snum == 9 );
$_ = $t;
s/\Ga/xx/;
ok( $_ eq 'xxaaaaaaaa' );
$_ = $t;
s/\Ga/x/;
ok( $_ eq 'xaaaaaaaa' );
$_ = 'aaaa';
$snum = s/\ba/./g;
ok( $_ eq '.aaa' && $snum == 1 );
eval q% s/a/"b"}/e %;
ok( $@ =~ /Bad evalled substitution/ );
eval q% ($_ = "x") =~ s/(.)/"$1 "/e %;
ok( $_ eq "x " and !length $@ );
$x = $x = 'interp';
eval q% ($_ = "x") =~ s/x(($x)*)/"$1"/e %;
ok( $_ eq '' and !length $@ );
$_ = "C:/";
ok( !s/^([a-z]:)/\u$1/ );
$_ = "Charles Bronson";
$snum = s/\B\w//g;
ok( $_ eq "C B" && $snum == 12 );
{
use utf8;
my $s = "H\303\266he";
my $l = my $r = $s;
$l =~ s/[^\w]//g;
$r =~ s/[^\w\.]//g;
is($l, $r, "use utf8 \\w");
}
my $pv1 = my $pv2 = "Andreas J. K\303\266nig";
$pv1 =~ s/A/\x{100}/;
substr($pv2,0,1) = "\x{100}";
is($pv1, $pv2);
SKIP: {
skip("EBCDIC", 3) if ord("A") == 193;
{
# Gregor Chrupala <gregor.chrupala@star-group.net>
use utf8;
$a = 'España';
$a =~ s/ñ/ñ/;
like($a, qr/ñ/, "use utf8 RHS");
}
{
use utf8;
$a = 'España España';
$a =~ s/ñ/ñ/;
like($a, qr/ñ/, "use utf8 LHS");
}
{
use utf8;
$a = 'España';
$a =~ s/ñ/ñ/;
like($a, qr/ñ/, "use utf8 LHS and RHS");
}
}
{
# SADAHIRO Tomoyuki <bqw10602@nifty.com>
$a = "\x{100}\x{101}";
$a =~ s/\x{101}/\xFF/;
like($a, qr/\xFF/);
is(length($a), 2, "SADAHIRO utf8 s///");
$a = "\x{100}\x{101}";
$a =~ s/\x{101}/"\xFF"/e;
like($a, qr/\xFF/);
is(length($a), 2);
$a = "\x{100}\x{101}";
$a =~ s/\x{101}/\xFF\xFF\xFF/;
like($a, qr/\xFF\xFF\xFF/);
is(length($a), 4);
$a = "\x{100}\x{101}";
$a =~ s/\x{101}/"\xFF\xFF\xFF"/e;
like($a, qr/\xFF\xFF\xFF/);
is(length($a), 4);
$a = "\xFF\x{101}";
$a =~ s/\xFF/\x{100}/;
like($a, qr/\x{100}/);
is(length($a), 2);
$a = "\xFF\x{101}";
$a =~ s/\xFF/"\x{100}"/e;
like($a, qr/\x{100}/);
is(length($a), 2);
$a = "\xFF";
$a =~ s/\xFF/\x{100}/;
like($a, qr/\x{100}/);
is(length($a), 1);
$a = "\xFF";
$a =~ s/\xFF/"\x{100}"/e;
like($a, qr/\x{100}/);
is(length($a), 1);
}
{
# subst with mixed utf8/non-utf8 type
my($ua, $ub, $uc, $ud) = ("\x{101}", "\x{102}", "\x{103}", "\x{104}");
my($na, $nb) = ("\x{ff}", "\x{fe}");
my $a = "$ua--$ub";
my $b;
($b = $a) =~ s/--/$na/;
is($b, "$ua$na$ub", "s///: replace non-utf8 into utf8");
($b = $a) =~ s/--/--$na--/;
is($b, "$ua--$na--$ub", "s///: replace long non-utf8 into utf8");
($b = $a) =~ s/--/$uc/;
is($b, "$ua$uc$ub", "s///: replace utf8 into utf8");
($b = $a) =~ s/--/--$uc--/;
is($b, "$ua--$uc--$ub", "s///: replace long utf8 into utf8");
$a = "$na--$nb";
($b = $a) =~ s/--/$ua/;
is($b, "$na$ua$nb", "s///: replace utf8 into non-utf8");
($b = $a) =~ s/--/--$ua--/;
is($b, "$na--$ua--$nb", "s///: replace long utf8 into non-utf8");
# now with utf8 pattern
$a = "$ua--$ub";
($b = $a) =~ s/-($ud)?-/$na/;
is($b, "$ua$na$ub", "s///: replace non-utf8 into utf8 (utf8 pattern)");
($b = $a) =~ s/-($ud)?-/--$na--/;
is($b, "$ua--$na--$ub", "s///: replace long non-utf8 into utf8 (utf8 pattern)");
($b = $a) =~ s/-($ud)?-/$uc/;
is($b, "$ua$uc$ub", "s///: replace utf8 into utf8 (utf8 pattern)");
($b = $a) =~ s/-($ud)?-/--$uc--/;
is($b, "$ua--$uc--$ub", "s///: replace long utf8 into utf8 (utf8 pattern)");
$a = "$na--$nb";
($b = $a) =~ s/-($ud)?-/$ua/;
is($b, "$na$ua$nb", "s///: replace utf8 into non-utf8 (utf8 pattern)");
($b = $a) =~ s/-($ud)?-/--$ua--/;
is($b, "$na--$ua--$nb", "s///: replace long utf8 into non-utf8 (utf8 pattern)");
($b = $a) =~ s/-($ud)?-/$na/;
is($b, "$na$na$nb", "s///: replace non-utf8 into non-utf8 (utf8 pattern)");
($b = $a) =~ s/-($ud)?-/--$na--/;
is($b, "$na--$na--$nb", "s///: replace long non-utf8 into non-utf8 (utf8 pattern)");
}
$_ = 'aaaa';
$r = 'x';
$s = s/a(?{})/$r/g;
is("<$_> <$s>", "<xxxx> <4>", "[perl #7806]");
$_ = 'aaaa';
$s = s/a(?{})//g;
is("<$_> <$s>", "<> <4>", "[perl #7806]");
# [perl #19048] Coredump in silly replacement
{
local $^W = 0;
$_="abcdef\n";
s!.!!eg;
is($_, "\n", "[perl #19048]");
}
# [perl #17757] interaction between saw_ampersand and study
{
my $f = eval q{ $& };
$f = "xx";
study $f;
$f =~ s/x/y/g;
is($f, "yy", "[perl #17757]");
}
# [perl #20684] returned a zero count
$_ = "1111";
is(s/(??{1})/2/eg, 4, '#20684 s/// with (??{..}) inside');
# [perl #20682] @- not visible in replacement
$_ = "123";
/(2)/; # seed @- with something else
s/(1)(2)(3)/$#- (@-)/;
is($_, "3 (0 0 1 2)", '#20682 @- not visible in replacement');
# [perl #20682] $^N not visible in replacement
$_ = "abc";
/(a)/; s/(b)|(c)/-$^N/g;
is($_,'a-b-c','#20682 $^N not visible in replacement');
# [perl #22351] perl bug with 'e' substitution modifier
my $name = "chris";
{
no warnings 'uninitialized';
$name =~ s/hr//e;
}
is($name, "cis", q[#22351 bug with 'e' substitution modifier]);
# [perl #34171] $1 didn't honour 'use bytes' in s//e
{
my $s="\x{100}";
my $x;
{
use bytes;
$s=~ s/(..)/$x=$1/e
}
is(length($x), 2, '[perl #34171]');
}
{ # [perl #27940] perlbug: [\x00-\x1f] works, [\c@-\c_] does not
my $c;
($c = "\x20\c@\x30\cA\x40\cZ\x50\c_\x60") =~ s/[\c@-\c_]//g;
is($c, "\x20\x30\x40\x50\x60", "s/[\\c\@-\\c_]//g");
($c = "\x20\x00\x30\x01\x40\x1A\x50\x1F\x60") =~ s/[\x00-\x1f]//g;
is($c, "\x20\x30\x40\x50\x60", "s/[\\x00-\\x1f]//g");
}
{
$_ = "xy";
no warnings 'uninitialized';
/(((((((((x)))))))))(z)/; # clear $10
s/(((((((((x)))))))))(y)/${10}/;
is($_,"y","RT#6006: \$_ eq '$_'");
$_ = "xr";
s/(((((((((x)))))))))(r)/fooba${10}/;
is($_,"foobar","RT#6006: \$_ eq '$_'");
}
{
my $want=("\n" x 11).("B\n" x 11)."B";
$_="B";
our $i;
for $i(1..11){
s/^.*$/$&/gm;
$_="\n$_\n$&";
}
is($want,$_,"RT#17542");
}
{
my @tests = ('ABC', "\xA3\xA4\xA5", "\x{410}\x{411}\x{412}");
foreach (@tests) {
my $id = ord $_;
s/./pos/ge;
is($_, "012", "RT#52104: $id");
}
}
fresh_perl_is( '$_=q(foo);s/(.)\G//g;print' => 'foo', '[perl #69056] positive GPOS regex segfault' );
fresh_perl_is( '$_="abcef"; s/bc|(.)\G(.)/$1 ? "[$1-$2]" : "XX"/ge; print' => 'aXX[c-e][e-f]f', 'positive GPOS regex substitution failure' );
# [perl #~~~~~] $var =~ s/$qr//e calling get-magic on $_ as well as $var
{
local *_;
my $scratch;
sub qrBug::TIESCALAR { bless[pop], 'qrBug' }
sub qrBug::FETCH { $scratch .= "[fetching $_[0][0]]"; 'prew' }
sub qrBug::STORE{}
tie my $kror, qrBug => '$kror';
tie $_, qrBug => '$_';
my $qr = qr/(?:)/;
$kror =~ s/$qr/""/e;
is(
$scratch, '[fetching $kror]',
'bug: $var =~ s/$qr//e calling get-magic on $_ as well as $var',
);
}
{ # Bug #41530; replacing non-utf8 with a utf8 causes problems
my $string = "a\x{a0}a";
my $sub_string = $string;
ok(! utf8::is_utf8($sub_string), "Verify that string isn't initially utf8");
$sub_string =~ s/a/\x{100}/g;
ok(utf8::is_utf8($sub_string),
'Verify replace of non-utf8 with utf8 upgrades to utf8');
is($sub_string, "\x{100}\x{A0}\x{100}",
'Verify #41530 fixed: replace of non-utf8 with utf8');
my $non_sub_string = $string;
ok(! utf8::is_utf8($non_sub_string),
"Verify that string isn't initially utf8");
$non_sub_string =~ s/b/\x{100}/g;
ok(! utf8::is_utf8($non_sub_string),
"Verify that failed substitute doesn't change string's utf8ness");
is($non_sub_string, $string,
"Verify that failed substitute doesn't change string");
}
{ # Verify largish octal in replacement pattern
my $string = "a";
$string =~ s/a/\400/;
is($string, chr 0x100, "Verify that handles s/foo/\\400/");
$string =~ s/./\600/;
is($string, chr 0x180, "Verify that handles s/foo/\\600/");
$string =~ s/./\777/;
is($string, chr 0x1FF, "Verify that handles s/foo/\\777/");
}
|