This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Slightly shorten most regex patterns
[perl5.git] / t / op / tr.t
CommitLineData
c8e3bb4c
GS
1# tr.t
2
cb6d3474
KW
3use utf8;
4
f05dd7cc
JH
5BEGIN {
6 chdir 't' if -d 't';
953ab6e5 7 require './test.pl';
43ece5b1 8 set_up_inc('../lib');
f05dd7cc 9}
a5095b95 10
a53bfdae 11plan tests => 138;
953ab6e5 12
f605e527
FC
13# Test this first before we extend the stack with other operations.
14# This caused an asan failure due to a bad write past the end of the stack.
15eval { my $x; die 1..127, $x =~ y/// };
16
c8e3bb4c
GS
17$_ = "abcdefghijklmnopqrstuvwxyz";
18
19tr/a-z/A-Z/;
20
953ab6e5 21is($_, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 'uc');
c8e3bb4c
GS
22
23tr/A-Z/a-z/;
24
953ab6e5 25is($_, "abcdefghijklmnopqrstuvwxyz", 'lc');
c8e3bb4c
GS
26
27tr/b-y/B-Y/;
953ab6e5 28is($_, "aBCDEFGHIJKLMNOPQRSTUVWXYz", 'partial uc');
c8e3bb4c 29
c8e3bb4c
GS
30
31# In EBCDIC 'I' is \xc9 and 'J' is \0xd1, 'i' is \x89 and 'j' is \x91.
32# Yes, discontinuities. Regardless, the \xca in the below should stay
33# untouched (and not became \x8a).
5e037136
GS
34{
35 no utf8;
36 $_ = "I\xcaJ";
c8e3bb4c 37
5e037136 38 tr/I-J/i-j/;
c8e3bb4c 39
ff36f15d 40 is($_, "i\xcaj", 'EBCDIC discontinuity');
5e037136 41}
c8e3bb4c 42#
2de7b02f 43
953ab6e5 44
2de7b02f
GS
45($x = 12) =~ tr/1/3/;
46(my $y = 12) =~ tr/1/3/;
47($f = 1.5) =~ tr/1/3/;
48(my $g = 1.5) =~ tr/1/3/;
953ab6e5
MS
49is($x + $y + $f + $g, 71, 'tr cancels IOK and NOK');
50
bb16bae8
FC
51# /r
52$_ = 'adam';
53is y/dam/ve/rd, 'eve', '/r';
54is $_, 'adam', '/r leaves param alone';
55$g = 'ruby';
56is $g =~ y/bury/repl/r, 'perl', '/r with explicit param';
57is $g, 'ruby', '/r leaves explicit param alone';
58is "aaa" =~ y\a\b\r, 'bbb', '/r with constant param';
59ok !eval '$_ !~ y///r', "!~ y///r is forbidden";
60like $@, qr\^Using !~ with tr///r doesn't make sense\,
61 "!~ y///r error message";
62{
63 my $w;
64 my $wc;
65 local $SIG{__WARN__} = sub { $w = shift; ++$wc };
66 local $^W = 1;
67 eval 'y///r; 1';
68 like $w, qr '^Useless use of non-destructive transliteration \(tr///r\)',
69 '/r warns in void context';
70 is $wc, 1, '/r warns just once';
71}
2de7b02f 72
953ab6e5 73# perlbug [ID 20000511.005]
2de7b02f
GS
74$_ = 'fred';
75/([a-z]{2})/;
76$1 =~ tr/A-Z//;
77s/^(\s*)f/$1F/;
953ab6e5
MS
78is($_, 'Fred', 'harmless if explicitly not updating');
79
80
81# A variant of the above, added in 5.7.2
82$_ = 'fred';
83/([a-z]{2})/;
84eval '$1 =~ tr/A-Z/A-Z/;';
85s/^(\s*)f/$1F/;
86is($_, 'Fred', 'harmless if implicitly not updating');
87is($@, '', ' no error');
88
2de7b02f
GS
89
90# check tr handles UTF8 correctly
91($x = 256.65.258) =~ tr/a/b/;
953ab6e5
MS
92is($x, 256.65.258, 'handles UTF8');
93is(length $x, 3);
94
2de7b02f 95$x =~ tr/A/B/;
953ab6e5 96is(length $x, 3);
83bcbc61 97if ($::IS_ASCII) { # ASCII
953ab6e5 98 is($x, 256.66.258);
67a17885
PP
99}
100else {
953ab6e5 101 is($x, 256.65.258);
67a17885 102}
953ab6e5 103
cbe7f703
PP
104# EBCDIC variants of the above tests
105($x = 256.193.258) =~ tr/a/b/;
953ab6e5
MS
106is(length $x, 3);
107is($x, 256.193.258);
108
cbe7f703 109$x =~ tr/A/B/;
953ab6e5 110is(length $x, 3);
83bcbc61 111if ($::IS_ASCII) { # ASCII
953ab6e5 112 is($x, 256.193.258);
cbe7f703
PP
113}
114else {
953ab6e5 115 is($x, 256.194.258);
cbe7f703 116}
953ab6e5 117
036b4402
GS
118
119{
953ab6e5
MS
120 my $l = chr(300); my $r = chr(400);
121 $x = 200.300.400;
122 $x =~ tr/\x{12c}/\x{190}/;
123 is($x, 200.400.400,
124 'changing UTF8 chars in a UTF8 string, same length');
125 is(length $x, 3);
126
127 $x = 200.300.400;
128 $x =~ tr/\x{12c}/\x{be8}/;
129 is($x, 200.3048.400, ' more bytes');
130 is(length $x, 3);
131
132 $x = 100.125.60;
133 $x =~ tr/\x{64}/\x{190}/;
134 is($x, 400.125.60, 'Putting UT8 chars into a non-UTF8 string');
135 is(length $x, 3);
136
137 $x = 400.125.60;
138 $x =~ tr/\x{190}/\x{64}/;
139 is($x, 100.125.60, 'Removing UTF8 chars from UTF8 string');
140 is(length $x, 3);
141
142 $x = 400.125.60.400;
143 $y = $x =~ tr/\x{190}/\x{190}/;
144 is($y, 2, 'Counting UTF8 chars in UTF8 string');
145
146 $x = 60.400.125.60.400;
147 $y = $x =~ tr/\x{3c}/\x{3c}/;
148 is($y, 2, ' non-UTF8 chars in UTF8 string');
149
150 # 17 - counting UTF8 chars in non-UTF8 string
151 $x = 200.125.60;
152 $y = $x =~ tr/\x{190}/\x{190}/;
153 is($y, 0, ' UTF8 chars in non-UTFs string');
036b4402 154}
c2e66d9e 155
c2e66d9e 156$_ = "abcdefghijklmnopqrstuvwxyz";
953ab6e5
MS
157eval 'tr/a-z-9/ /';
158like($@, qr/^Ambiguous range in transliteration operator/, 'tr/a-z-9//');
c2e66d9e 159
cbe7f703 160# 19-21: Make sure leading and trailing hyphens still work
c2e66d9e
GS
161$_ = "car-rot9";
162tr/-a-m/./;
953ab6e5 163is($_, '..r.rot9', 'hyphens, leading');
c2e66d9e
GS
164
165$_ = "car-rot9";
166tr/a-m-/./;
953ab6e5 167is($_, '..r.rot9', ' trailing');
c2e66d9e
GS
168
169$_ = "car-rot9";
170tr/-a-m-/./;
953ab6e5 171is($_, '..r.rot9', ' both');
c2e66d9e
GS
172
173$_ = "abcdefghijklmnop";
174tr/ae-hn/./;
953ab6e5 175is($_, '.bcd....ijklm.op');
c2e66d9e
GS
176
177$_ = "abcdefghijklmnop";
178tr/a-cf-kn-p/./;
953ab6e5 179is($_, '...de......lm...');
c2e66d9e
GS
180
181$_ = "abcdefghijklmnop";
182tr/a-ceg-ikm-o/./;
953ab6e5
MS
183is($_, '...d.f...j.l...p');
184
c2e66d9e 185
c2e66d9e
GS
186# 20000705 MJD
187eval "tr/m-d/ /";
321ecc04 188like($@, qr/^Invalid range "m-d" in transliteration operator/,
953ab6e5 189 'reversed range check');
c2e66d9e 190
d897a58d 191'abcdef' =~ /(bcd)/;
953ab6e5
MS
192is(eval '$1 =~ tr/abcd//', 3, 'explicit read-only count');
193is($@, '', ' no error');
d897a58d 194
953ab6e5
MS
195'abcdef' =~ /(bcd)/;
196is(eval '$1 =~ tr/abcd/abcd/', 3, 'implicit read-only count');
197is($@, '', ' no error');
198
199is(eval '"123" =~ tr/12//', 2, 'LHS of non-updating tr');
d897a58d 200
94bfe852 201eval '"123" =~ tr/1/2/';
953ab6e5
MS
202like($@, qr|^Can't modify constant item in transliteration \(tr///\)|,
203 'LHS bad on updating tr');
204
d897a58d 205
381d18bc
JH
206# v300 (0x12c) is UTF-8-encoded as 196 172 (0xc4 0xac)
207# v400 (0x190) is UTF-8-encoded as 198 144 (0xc6 0x90)
208
209# Transliterate a byte to a byte, all four ways.
210
211($a = v300.196.172.300.196.172) =~ tr/\xc4/\xc5/;
953ab6e5 212is($a, v300.197.172.300.197.172, 'byte2byte transliteration');
381d18bc
JH
213
214($a = v300.196.172.300.196.172) =~ tr/\xc4/\x{c5}/;
953ab6e5 215is($a, v300.197.172.300.197.172);
381d18bc
JH
216
217($a = v300.196.172.300.196.172) =~ tr/\x{c4}/\xc5/;
953ab6e5 218is($a, v300.197.172.300.197.172);
381d18bc
JH
219
220($a = v300.196.172.300.196.172) =~ tr/\x{c4}/\x{c5}/;
953ab6e5 221is($a, v300.197.172.300.197.172);
381d18bc 222
381d18bc
JH
223
224($a = v300.196.172.300.196.172) =~ tr/\xc4/\x{12d}/;
953ab6e5 225is($a, v300.301.172.300.301.172, 'byte2wide transliteration');
381d18bc
JH
226
227($a = v300.196.172.300.196.172) =~ tr/\x{12c}/\xc3/;
953ab6e5 228is($a, v195.196.172.195.196.172, ' wide2byte');
381d18bc
JH
229
230($a = v300.196.172.300.196.172) =~ tr/\x{12c}/\x{12d}/;
953ab6e5 231is($a, v301.196.172.301.196.172, ' wide2wide');
381d18bc 232
381d18bc
JH
233
234($a = v300.196.172.300.196.172) =~ tr/\xc4\x{12c}/\x{12d}\xc3/;
953ab6e5 235is($a, v195.301.172.195.301.172, 'byte2wide & wide2byte');
381d18bc 236
381d18bc
JH
237
238($a = v300.196.172.300.196.172.400.198.144) =~
239 tr/\xac\xc4\x{12c}\x{190}/\xad\x{12d}\xc5\x{191}/;
953ab6e5 240is($a, v197.301.173.197.301.173.401.198.144, 'all together now!');
381d18bc 241
381d18bc 242
953ab6e5
MS
243is((($a = v300.196.172.300.196.172) =~ tr/\xc4/\xc5/), 2,
244 'transliterate and count');
381d18bc 245
953ab6e5 246is((($a = v300.196.172.300.196.172) =~ tr/\x{12c}/\x{12d}/), 2);
381d18bc 247
381d18bc
JH
248
249($a = v300.196.172.300.196.172) =~ tr/\xc4/\x{12d}/c;
953ab6e5 250is($a, v301.196.301.301.196.301, 'translit w/complement');
381d18bc
JH
251
252($a = v300.196.172.300.196.172) =~ tr/\x{12c}/\xc5/c;
953ab6e5 253is($a, v300.197.197.300.197.197);
381d18bc 254
381d18bc
JH
255
256($a = v300.196.172.300.196.172) =~ tr/\xc4//d;
953ab6e5 257is($a, v300.172.300.172, 'translit w/deletion');
381d18bc
JH
258
259($a = v300.196.172.300.196.172) =~ tr/\x{12c}//d;
953ab6e5 260is($a, v196.172.196.172);
381d18bc 261
381d18bc
JH
262
263($a = v196.196.172.300.300.196.172) =~ tr/\xc4/\xc5/s;
953ab6e5 264is($a, v197.172.300.300.197.172, 'translit w/squeeze');
381d18bc
JH
265
266($a = v196.172.300.300.196.172.172) =~ tr/\x{12c}/\x{12d}/s;
953ab6e5 267is($a, v196.172.301.196.172.172);
381d18bc 268
a1874b66 269
953ab6e5 270# Tricky cases (When Simon Cozens Attacks)
a1874b66 271($a = v196.172.200) =~ tr/\x{12c}/a/;
953ab6e5 272is(sprintf("%vd", $a), '196.172.200');
a1874b66
JH
273
274($a = v196.172.200) =~ tr/\x{12c}/\x{12c}/;
953ab6e5 275is(sprintf("%vd", $a), '196.172.200');
a1874b66
JH
276
277($a = v196.172.200) =~ tr/\x{12c}//d;
953ab6e5
MS
278is(sprintf("%vd", $a), '196.172.200');
279
a1874b66 280
8973db79 281# UTF8 range tests from Inaba Hiroto
f9a63242 282
a26bfc40 283# Not working in EBCDIC as of 12674.
f9a63242 284($a = v300.196.172.302.197.172) =~ tr/\x{12c}-\x{130}/\xc0-\xc4/;
953ab6e5 285is($a, v192.196.172.194.197.172, 'UTF range');
f9a63242
JH
286
287($a = v300.196.172.302.197.172) =~ tr/\xc4-\xc8/\x{12c}-\x{130}/;
953ab6e5
MS
288is($a, v300.300.172.302.301.172);
289
8973db79
JH
290
291# UTF8 range tests from Karsten Sperling (patch #9008 required)
292
293($a = "\x{0100}") =~ tr/\x00-\x{100}/X/;
953ab6e5 294is($a, "X");
8973db79
JH
295
296($a = "\x{0100}") =~ tr/\x{0000}-\x{00ff}/X/c;
953ab6e5 297is($a, "X");
8973db79
JH
298
299($a = "\x{0100}") =~ tr/\x{0000}-\x{00ff}\x{0101}/X/c;
953ab6e5 300is($a, "X");
8973db79
JH
301
302($a = v256) =~ tr/\x{0000}-\x{00ff}\x{0101}/X/c;
953ab6e5
MS
303is($a, "X");
304
8973db79 305
94472101
JH
306# UTF8 range tests from Inaba Hiroto
307
308($a = "\x{200}") =~ tr/\x00-\x{100}/X/c;
953ab6e5 309is($a, "X");
94472101
JH
310
311($a = "\x{200}") =~ tr/\x00-\x{100}/X/cs;
953ab6e5
MS
312is($a, "X");
313
685b06b5
KW
314# Tricky on EBCDIC: while [a-z] [A-Z] must not match the gap characters (as
315# well as i-j, r-s, I-J, R-S), [\x89-\x91] [\xc9-\xd1] has to match them,
6b6bd37b
JH
316# from Karsten Sperling.
317
318$c = ($a = "\x89\x8a\x8b\x8c\x8d\x8f\x90\x91") =~ tr/\x89-\x91/X/;
953ab6e5
MS
319is($c, 8);
320is($a, "XXXXXXXX");
4c3a8340 321
6b6bd37b 322$c = ($a = "\xc9\xca\xcb\xcc\xcd\xcf\xd0\xd1") =~ tr/\xc9-\xd1/X/;
953ab6e5
MS
323is($c, 8);
324is($a, "XXXXXXXX");
6b6bd37b 325
4c3a8340 326SKIP: {
02f1786b 327 skip "valid only on EBCDIC platforms", 4 unless $::IS_EBCDIC;
953ab6e5
MS
328
329 $c = ($a = "\x89\x8a\x8b\x8c\x8d\x8f\x90\x91") =~ tr/i-j/X/;
330 is($c, 2);
331 is($a, "X\x8a\x8b\x8c\x8d\x8f\x90X");
332
333 $c = ($a = "\xc9\xca\xcb\xcc\xcd\xcf\xd0\xd1") =~ tr/I-J/X/;
334 is($c, 2);
335 is($a, "X\xca\xcb\xcc\xcd\xcf\xd0X");
6b6bd37b 336}
1ed601ec
JH
337
338($a = "\x{100}") =~ tr/\x00-\xff/X/c;
953ab6e5 339is(ord($a), ord("X"));
1ed601ec
JH
340
341($a = "\x{100}") =~ tr/\x00-\xff/X/cs;
953ab6e5 342is(ord($a), ord("X"));
1ed601ec
JH
343
344($a = "\x{100}\x{100}") =~ tr/\x{101}-\x{200}//c;
953ab6e5 345is($a, "\x{100}\x{100}");
1ed601ec
JH
346
347($a = "\x{100}\x{100}") =~ tr/\x{101}-\x{200}//cs;
953ab6e5 348is($a, "\x{100}");
1ed601ec 349
629b4584 350$a = "\xfe\xff"; $a =~ tr/\xfe\xff/\x{1ff}\x{1fe}/;
953ab6e5
MS
351is($a, "\x{1ff}\x{1fe}");
352
76ef7183
JH
353
354# From David Dyck
355($a = "R0_001") =~ tr/R_//d;
953ab6e5 356is(hex($a), 1);
76ef7183 357
800b4dc4
JH
358# From Inaba Hiroto
359@a = (1,2); map { y/1/./ for $_ } @a;
953ab6e5 360is("@a", ". 2");
800b4dc4
JH
361
362@a = (1,2); map { y/1/./ for $_.'' } @a;
953ab6e5
MS
363is("@a", "1 2");
364
800b4dc4 365
bec89253
RH
366# Additional test for Inaba Hiroto patch (robin@kitsite.com)
367($a = "\x{100}\x{102}\x{101}") =~ tr/\x00-\377/XYZ/c;
953ab6e5
MS
368is($a, "XZY");
369
bec89253 370
2233f375
NC
371# Used to fail with "Modification of a read-only value attempted"
372%a = (N=>1);
373foreach (keys %a) {
953ab6e5
MS
374 eval 'tr/N/n/';
375 is($_, 'n', 'pp_trans needs to unshare shared hash keys');
376 is($@, '', ' no error');
2233f375 377}
94bfe852 378
953ab6e5 379
94bfe852 380$x = eval '"1213" =~ tr/1/1/';
953ab6e5
MS
381is($x, 2, 'implicit count on constant');
382is($@, '', ' no error');
383
384
385my @foo = ();
386eval '$foo[-1] =~ tr/N/N/';
387is( $@, '', 'implicit count outside array bounds, index negative' );
388is( scalar @foo, 0, " doesn't extend the array");
389
390eval '$foo[1] =~ tr/N/N/';
391is( $@, '', 'implicit count outside array bounds, index positive' );
392is( scalar @foo, 0, " doesn't extend the array");
393
394
395my %foo = ();
396eval '$foo{bar} =~ tr/N/N/';
397is( $@, '', 'implicit count outside hash bounds' );
398is( scalar keys %foo, 0, " doesn't extend the hash");
d59e14db
RGS
399
400$x = \"foo";
401is( $x =~ tr/A/A/, 2, 'non-modifying tr/// on a scalar ref' );
402is( ref $x, 'SCALAR', " doesn't stringify its argument" );
0d65d7d5
MS
403
404# rt.perl.org 36622. Perl didn't like a y/// at end of file. No trailing
405# newline allowed.
406fresh_perl_is(q[$_ = "foo"; y/A-Z/a-z/], '');
9f7f3913
TS
407
408
409{ # [perl #38293] chr(65535) should be allowed in regexes
410no warnings 'utf8'; # to allow non-characters
411
412$s = "\x{d800}\x{ffff}";
413$s =~ tr/\0/A/;
414is($s, "\x{d800}\x{ffff}", "do_trans_simple");
415
416$s = "\x{d800}\x{ffff}";
417$i = $s =~ tr/\0//;
418is($i, 0, "do_trans_count");
419
420$s = "\x{d800}\x{ffff}";
421$s =~ tr/\0/A/s;
422is($s, "\x{d800}\x{ffff}", "do_trans_complex, SQUASH");
423
424$s = "\x{d800}\x{ffff}";
425$s =~ tr/\0/A/c;
426is($s, "AA", "do_trans_complex, COMPLEMENT");
427
428$s = "A\x{ffff}B";
429$s =~ tr/\x{ffff}/\x{1ffff}/;
430is($s, "A\x{1ffff}B", "utf8, SEARCHLIST");
431
432$s = "\x{fffd}\x{fffe}\x{ffff}";
433$s =~ tr/\x{fffd}-\x{ffff}/ABC/;
434is($s, "ABC", "utf8, SEARCHLIST range");
435
436$s = "ABC";
437$s =~ tr/ABC/\x{ffff}/;
438is($s, "\x{ffff}"x3, "utf8, REPLACEMENTLIST");
439
440$s = "ABC";
441$s =~ tr/ABC/\x{fffd}-\x{ffff}/;
442is($s, "\x{fffd}\x{fffe}\x{ffff}", "utf8, REPLACEMENTLIST range");
443
444$s = "A\x{ffff}B\x{100}\0\x{fffe}\x{ffff}";
445$i = $s =~ tr/\x{ffff}//;
446is($i, 2, "utf8, count");
447
448$s = "A\x{ffff}\x{ffff}C";
449$s =~ tr/\x{ffff}/\x{100}/s;
450is($s, "A\x{100}C", "utf8, SQUASH");
451
452$s = "A\x{ffff}\x{ffff}\x{fffe}\x{fffe}\x{fffe}C";
453$s =~ tr/\x{fffe}\x{ffff}//s;
454is($s, "A\x{ffff}\x{fffe}C", "utf8, SQUASH");
455
456$s = "xAABBBy";
457$s =~ tr/AB/\x{ffff}/s;
458is($s, "x\x{ffff}y", "utf8, SQUASH");
459
460$s = "xAABBBy";
461$s =~ tr/AB/\x{fffe}\x{ffff}/s;
462is($s, "x\x{fffe}\x{ffff}y", "utf8, SQUASH");
463
464$s = "A\x{ffff}B\x{fffe}C";
465$s =~ tr/\x{fffe}\x{ffff}/x/c;
466is($s, "x\x{ffff}x\x{fffe}x", "utf8, COMPLEMENT");
467
468$s = "A\x{10000}B\x{2abcd}C";
469$s =~ tr/\0-\x{ffff}/x/c;
470is($s, "AxBxC", "utf8, COMPLEMENT range");
471
472$s = "A\x{fffe}B\x{ffff}C";
473$s =~ tr/\x{fffe}\x{ffff}/x/d;
474is($s, "AxBC", "utf8, DELETE");
475
476} # non-characters end
477
1749ea0d
TS
478{ # related to [perl #27940]
479 my $c;
480
481 ($c = "\x20\c@\x30\cA\x40\cZ\x50\c_\x60") =~ tr/\c@-\c_//d;
482 is($c, "\x20\x30\x40\x50\x60", "tr/\\c\@-\\c_//d");
483
484 ($c = "\x20\x00\x30\x01\x40\x1A\x50\x1F\x60") =~ tr/\x00-\x1f//d;
485 is($c, "\x20\x30\x40\x50\x60", "tr/\\x00-\\x1f//d");
486}
487
3788ef8f 488($s) = keys %{{pie => 3}};
3e89ba19 489SKIP: {
e3918bb7
FC
490 if (!eval { require XS::APItest }) { skip "no XS::APItest", 2 }
491 my $wasro = XS::APItest::SvIsCOW($s);
2203fb5a 492 ok $wasro, "have a COW";
3788ef8f 493 $s =~ tr/i//;
e3918bb7 494 ok( XS::APItest::SvIsCOW($s),
3e89ba19 495 "count-only tr doesn't deCOW COWs" );
3788ef8f 496}
a5446a64
DM
497
498# [ RT #61520 ]
499#
500# under threads, unicode tr within a cloned closure would SEGV or assert
501# fail, since the pointer in the pad to the swash was getting zeroed out
502# in the proto-CV
503
504{
505 my $x = "\x{142}";
506 sub {
507 $x =~ tr[\x{142}][\x{143}];
508 }->();
509 is($x,"\x{143}", "utf8 + closure");
510}
511
9100eeb1
Z
512# Freeing of trans ops prior to pmtrans() [perl #102858].
513eval q{ $a ~= tr/a/b/; };
514ok 1;
515SKIP: {
55673181 516 no warnings "deprecated";
9100eeb1
Z
517 skip "no encoding", 1 unless eval { require encoding; 1 };
518 eval q{ use encoding "utf8"; $a ~= tr/a/b/; };
519 ok 1;
520}
a5446a64 521
cb6d3474
KW
522{ # [perl #113584]
523
524 my $x = "Perlα";
525 $x =~ tr/αα/βγ/;
baacc348 526 { no warnings 'utf8'; print "# $x\n"; } # No note() to avoid wide warning.
cb6d3474
KW
527 is($x, "Perlβ", "Only first of multiple transliterations is used");
528}
529
d8b516a1
FC
530# tr/a/b/ should fail even on zero-length read-only strings
531use constant nullrocow => (keys%{{""=>undef}})[0];
532for ("", nullrocow) {
533 eval { $_ =~ y/a/b/ };
534 like $@, qr/^Modification of a read-only value attempted at /,
535 'tr/a/b/ fails on zero-length ro string';
536}
537
a53bfdae
HS
538# Whether they're permitted or not, non-modifying tr/// should not write
539# to read-only values, even with funky flags.
540{ # [perl #123759]
541 eval q{ ('a' =~ /./) =~ tr///d };
542 ok(1, "tr///d on PL_Yes does not assert");
543 eval q{ ('a' =~ /./) =~ tr/a-z/a-z/d };
544 ok(1, "tr/a-z/a-z/d on PL_Yes does not assert");
545 eval q{ ('a' =~ /./) =~ tr///s };
546 ok(1, "tr///s on PL_Yes does not assert");
547 eval q{ *x =~ tr///d };
548 ok(1, "tr///d on glob does not assert");
549}
550
9100eeb1 5511;