This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Remove support for Splint static source code analyzer
[perl5.git] / t / op / split.t
CommitLineData
8d063cd8
LW
1#!./perl
2
a8a2fe91
JH
3BEGIN {
4 chdir 't' if -d 't';
3a2263fe 5 require './test.pl';
74f6d97d 6 require './charset_tools.pl';
43ece5b1 7 set_up_inc('../lib');
a8a2fe91
JH
8}
9
692044df 10plan tests => 159;
8d063cd8
LW
11
12$FS = ':';
13
14$_ = 'a:b:c';
15
16($a,$b,$c) = split($FS,$_);
17
acb447f4 18is(join(';',$a,$b,$c), 'a;b;c', 'Split a simple string into scalars.');
8d063cd8
LW
19
20@ary = split(/:b:/);
c1a7495a 21$cnt = split(/:b:/);
3a2263fe 22is(join("$_",@ary), 'aa:b:cc');
c1a7495a 23is($cnt, scalar(@ary));
8d063cd8
LW
24
25$_ = "abc\n";
4765795a 26my @xyz = (@ary = split(//));
c1a7495a 27$cnt = split(//);
3a2263fe 28is(join(".",@ary), "a.b.c.\n");
c1a7495a 29is($cnt, scalar(@ary));
8d063cd8
LW
30
31$_ = "a:b:c::::";
32@ary = split(/:/);
c1a7495a 33$cnt = split(/:/);
3a2263fe 34is(join(".",@ary), "a.b.c");
c1a7495a 35is($cnt, scalar(@ary));
2e1b3b7e 36
378cc40b 37$_ = join(':',split(' '," a b\tc \t d "));
3a2263fe 38is($_, 'a:b:c:d');
c1a7495a
BB
39@ary = split(' '," a b\tc \t d ");
40$cnt = split(' '," a b\tc \t d ");
41is($cnt, scalar(@ary));
2e1b3b7e
KK
42
43$_ = join(':',split(/ */,"foo bar bie\tdoll"));
3a2263fe 44is($_ , "f:o:o:b:a:r:b:i:e:\t:d:o:l:l");
c1a7495a
BB
45@ary = split(/ */,"foo bar bie\tdoll");
46$cnt = split(/ */,"foo bar bie\tdoll");
47is($cnt, scalar(@ary));
378cc40b
LW
48
49$_ = join(':', 'foo', split(/ /,'a b c'), 'bar');
3a2263fe 50is($_, "foo:a:b::c:bar");
c1a7495a
BB
51@ary = split(/ /,'a b c');
52$cnt = split(/ /,'a b c');
53is($cnt, scalar(@ary));
378cc40b 54
a687059c
LW
55# Can we say how many fields to split to?
56$_ = join(':', split(' ','1 2 3 4 5 6', 3));
acb447f4 57is($_, '1:2:3 4 5 6', "Split into a specified number of fields, defined by a literal");
c1a7495a
BB
58@ary = split(' ','1 2 3 4 5 6', 3);
59$cnt = split(' ','1 2 3 4 5 6', 3);
acb447f4 60is($cnt, scalar(@ary), "Check element count from previous test");
a687059c
LW
61
62# Can we do it as a variable?
63$x = 4;
64$_ = join(':', split(' ','1 2 3 4 5 6', $x));
acb447f4 65is($_, '1:2:3:4 5 6', "Split into a specified number of fields, defined by a scalar variable");
c1a7495a
BB
66@ary = split(' ','1 2 3 4 5 6', $x);
67$cnt = split(' ','1 2 3 4 5 6', $x);
acb447f4 68is($cnt, scalar(@ary), "Check element count from previous test");
a687059c
LW
69
70# Does the 999 suppress null field chopping?
71$_ = join(':', split(/:/,'1:2:3:4:5:6:::', 999));
3a2263fe 72is($_ , '1:2:3:4:5:6:::');
c1a7495a
BB
73@ary = split(/:/,'1:2:3:4:5:6:::', 999);
74$cnt = split(/:/,'1:2:3:4:5:6:::', 999);
75is($cnt, scalar(@ary));
a687059c 76
a720890c
GG
77# Splitting without pattern
78$_ = "1 2 3 4";
79$_ = join(':', split);
acb447f4 80is($_ , '1:2:3:4', "Split and join without specifying a split pattern");
a720890c 81
a687059c 82# Does assignment to a list imply split to one more field than that?
6cefa69e
RU
83$foo = runperl( switches => ['-Dt'], stderr => 1, prog => '($a,$b)=split;' );
84ok($foo =~ /DEBUGGING/ || $foo =~ /const\n?\Q(IV(3))\E/);
a687059c
LW
85
86# Can we say how many fields to split to when assigning to a list?
87($a,$b) = split(' ','1 2 3 4 5 6', 2);
88$_ = join(':',$a,$b);
acb447f4 89is($_, '1:2 3 4 5 6', "Storing split output into list of scalars");
a687059c 90
084811a7 91# do subpatterns generate additional fields (without trailing nulls)?
92$_ = join '|', split(/,|(-)/, "1-10,20,,,");
3a2263fe 93is($_, "1|-|10||20");
c1a7495a
BB
94@ary = split(/,|(-)/, "1-10,20,,,");
95$cnt = split(/,|(-)/, "1-10,20,,,");
96is($cnt, scalar(@ary));
084811a7 97
98# do subpatterns generate additional fields (with a limit)?
99$_ = join '|', split(/,|(-)/, "1-10,20,,,", 10);
3a2263fe 100is($_, "1|-|10||20||||||");
c1a7495a
BB
101@ary = split(/,|(-)/, "1-10,20,,,", 10);
102$cnt = split(/,|(-)/, "1-10,20,,,", 10);
103is($cnt, scalar(@ary));
e1fa4fd3
HS
104
105# is the 'two undefs' bug fixed?
106(undef, $a, undef, $b) = qw(1 2 3 4);
3a2263fe 107is("$a|$b", "2|4");
e1fa4fd3
HS
108
109# .. even for locals?
110{
111 local(undef, $a, undef, $b) = qw(1 2 3 4);
3a2263fe 112 is("$a|$b", "2|4");
e1fa4fd3 113}
fb73857a 114
115# check splitting of null string
116$_ = join('|', split(/x/, '',-1), 'Z');
3a2263fe 117is($_, "Z");
c1a7495a
BB
118@ary = split(/x/, '',-1);
119$cnt = split(/x/, '',-1);
120is($cnt, scalar(@ary));
fb73857a 121
122$_ = join('|', split(/x/, '', 1), 'Z');
3a2263fe 123is($_, "Z");
c1a7495a
BB
124@ary = split(/x/, '', 1);
125$cnt = split(/x/, '', 1);
126is($cnt, scalar(@ary));
fb73857a 127
128$_ = join('|', split(/(p+)/,'',-1), 'Z');
3a2263fe 129is($_, "Z");
c1a7495a
BB
130@ary = split(/(p+)/,'',-1);
131$cnt = split(/(p+)/,'',-1);
132is($cnt, scalar(@ary));
fb73857a 133
134$_ = join('|', split(/.?/, '',-1), 'Z');
3a2263fe 135is($_, "Z");
c1a7495a
BB
136@ary = split(/.?/, '',-1);
137$cnt = split(/.?/, '',-1);
138is($cnt, scalar(@ary));
fb73857a 139
c277df42
IZ
140
141# Are /^/m patterns scanned?
142$_ = join '|', split(/^a/m, "a b a\na d a", 20);
3a2263fe 143is($_, "| b a\n| d a");
c1a7495a
BB
144@ary = split(/^a/m, "a b a\na d a", 20);
145$cnt = split(/^a/m, "a b a\na d a", 20);
146is($cnt, scalar(@ary));
c277df42
IZ
147
148# Are /$/m patterns scanned?
149$_ = join '|', split(/a$/m, "a b a\na d a", 20);
3a2263fe 150is($_, "a b |\na d |");
c1a7495a
BB
151@ary = split(/a$/m, "a b a\na d a", 20);
152$cnt = split(/a$/m, "a b a\na d a", 20);
153is($cnt, scalar(@ary));
c277df42
IZ
154
155# Are /^/m patterns scanned?
156$_ = join '|', split(/^aa/m, "aa b aa\naa d aa", 20);
3a2263fe 157is($_, "| b aa\n| d aa");
c1a7495a
BB
158@ary = split(/^aa/m, "aa b aa\naa d aa", 20);
159$cnt = split(/^aa/m, "aa b aa\naa d aa", 20);
160is($cnt, scalar(@ary));
c277df42
IZ
161
162# Are /$/m patterns scanned?
163$_ = join '|', split(/aa$/m, "aa b aa\naa d aa", 20);
3a2263fe 164is($_, "aa b |\naa d |");
c1a7495a
BB
165@ary = split(/aa$/m, "aa b aa\naa d aa", 20);
166$cnt = split(/aa$/m, "aa b aa\naa d aa", 20);
167is($cnt, scalar(@ary));
c277df42
IZ
168
169# Greedyness:
170$_ = "a : b :c: d";
171@ary = split(/\s*:\s*/);
c1a7495a 172$cnt = split(/\s*:\s*/);
3a2263fe 173is(($res = join(".",@ary)), "a.b.c.d", $res);
c1a7495a 174is($cnt, scalar(@ary));
815d35b9
MG
175
176# use of match result as pattern (!)
3a2263fe 177is('p:q:r:s', join ':', split('abc' =~ /b/, 'p1q1r1s'));
c1a7495a
BB
178@ary = split('abc' =~ /b/, 'p1q1r1s');
179$cnt = split('abc' =~ /b/, 'p1q1r1s');
180is($cnt, scalar(@ary));
1ec94568
MG
181
182# /^/ treated as /^/m
183$_ = join ':', split /^/, "ab\ncd\nef\n";
1645b83c
YO
184is($_, "ab\n:cd\n:ef\n","check that split /^/ is treated as split /^/m");
185
186$_ = join ':', split /\A/, "ab\ncd\nef\n";
187is($_, "ab\ncd\nef\n","check that split /\A/ is NOT treated as split /^/m");
b3f5893f
GS
188
189# see if @a = @b = split(...) optimization works
190@list1 = @list2 = split ('p',"a p b c p");
3a2263fe
RGS
191ok(@list1 == @list2 &&
192 "@list1" eq "@list2" &&
193 @list1 == 2 &&
194 "@list1" eq "a b c ");
0156e0fd
RB
195
196# zero-width assertion
197$_ = join ':', split /(?=\w)/, "rm b";
3a2263fe 198is($_, "r:m :b");
c1a7495a
BB
199@ary = split /(?=\w)/, "rm b";
200$cnt = split /(?=\w)/, "rm b";
201is($cnt, scalar(@ary));
5a2d9fa2
JH
202
203# unicode splittage
974f237a 204
5a2d9fa2 205@ary = map {ord} split //, v1.20.300.4000.50000.4000.300.20.1;
c1a7495a 206$cnt = split //, v1.20.300.4000.50000.4000.300.20.1;
3a2263fe 207is("@ary", "1 20 300 4000 50000 4000 300 20 1");
c1a7495a 208is($cnt, scalar(@ary));
974f237a 209
ee95e30c
FC
210@ary = split(/\x{FE}/, "\x{FF}\x{FE}\x{FD}"); # bug id 20010105.016 (#5088)
211$cnt = split(/\x{FE}/, "\x{FF}\x{FE}\x{FD}"); # bug id 20010105.016 (#5088)
3a2263fe
RGS
212ok(@ary == 2 &&
213 $ary[0] eq "\xFF" && $ary[1] eq "\xFD" &&
214 $ary[0] eq "\x{FF}" && $ary[1] eq "\x{FD}");
c1a7495a 215is($cnt, scalar(@ary));
974f237a
JH
216
217@ary = split(/(\x{FE}\xFE)/, "\xFF\x{FF}\xFE\x{FE}\xFD\x{FD}"); # variant of 31
c1a7495a 218$cnt = split(/(\x{FE}\xFE)/, "\xFF\x{FF}\xFE\x{FE}\xFD\x{FD}"); # variant of 31
3a2263fe
RGS
219ok(@ary == 3 &&
220 $ary[0] eq "\xFF\xFF" &&
221 $ary[0] eq "\x{FF}\xFF" &&
222 $ary[0] eq "\x{FF}\x{FF}" &&
223 $ary[1] eq "\xFE\xFE" &&
224 $ary[1] eq "\x{FE}\xFE" &&
225 $ary[1] eq "\x{FE}\x{FE}" &&
226 $ary[2] eq "\xFD\xFD" &&
227 $ary[2] eq "\x{FD}\xFD" &&
228 $ary[2] eq "\x{FD}\x{FD}");
c1a7495a 229is($cnt, scalar(@ary));
4765795a
JH
230
231{
232 my @a = map ord, split(//, join("", map chr, (1234, 123, 2345)));
c1a7495a 233 my $c = split(//, join("", map chr, (1234, 123, 2345)));
3a2263fe 234 is("@a", "1234 123 2345");
c1a7495a 235 is($c, scalar(@a));
4765795a
JH
236}
237
238{
31e261c7
JH
239 my $x = 'A';
240 my @a = map ord, split(/$x/, join("", map chr, (1234, ord($x), 2345)));
c1a7495a 241 my $c = split(/$x/, join("", map chr, (1234, ord($x), 2345)));
3a2263fe 242 is("@a", "1234 2345");
c1a7495a 243 is($c, scalar(@a));
4765795a
JH
244}
245
246{
ee95e30c 247 # bug id 20000427.003 (#3173)
4765795a
JH
248
249 use warnings;
250 use strict;
251
252 my $sushi = "\x{b36c}\x{5a8c}\x{ff5b}\x{5079}\x{505b}";
253
254 my @charlist = split //, $sushi;
c1a7495a
BB
255 my $charnum = split //, $sushi;
256 is($charnum, scalar(@charlist));
4765795a
JH
257 my $r = '';
258 foreach my $ch (@charlist) {
259 $r = $r . " " . sprintf "U+%04X", ord($ch);
260 }
261
3a2263fe 262 is($r, " U+B36C U+5A8C U+FF5B U+5079 U+505B");
4765795a
JH
263}
264
265{
dd83d948
DD
266 my $s = "\x20\x40\x{80}\x{100}\x{80}\x40\x20";
267
74f6d97d 268 {
ee95e30c 269 # bug id 20000426.003 (#3166)
4765795a 270
31e261c7 271 my ($a, $b, $c) = split(/\x40/, $s);
3a2263fe 272 ok($a eq "\x20" && $b eq "\x{80}\x{100}\x{80}" && $c eq $a);
3a2263fe 273 }
4765795a
JH
274
275 my ($a, $b) = split(/\x{100}/, $s);
3a2263fe 276 ok($a eq "\x20\x40\x{80}" && $b eq "\x{80}\x40\x20");
4765795a
JH
277
278 my ($a, $b) = split(/\x{80}\x{100}\x{80}/, $s);
3a2263fe 279 ok($a eq "\x20\x40" && $b eq "\x40\x20");
4765795a 280
74f6d97d 281 {
31e261c7 282 my ($a, $b) = split(/\x40\x{80}/, $s);
3a2263fe 283 ok($a eq "\x20" && $b eq "\x{100}\x{80}\x40\x20");
3a2263fe 284 }
4765795a
JH
285
286 my ($a, $b, $c) = split(/[\x40\x{80}]+/, $s);
3a2263fe 287 ok($a eq "\x20" && $b eq "\x{100}" && $c eq "\x20");
4765795a
JH
288}
289
290{
ee95e30c 291 # 20001205.014 (#4844)
4765795a
JH
292
293 my $a = "ABC\x{263A}";
294
295 my @b = split( //, $a );
c1a7495a
BB
296 my $c = split( //, $a );
297 is($c, scalar(@b));
4765795a 298
3a2263fe 299 is(scalar @b, 4);
4765795a 300
3a2263fe 301 ok(length($b[3]) == 1 && $b[3] eq "\x{263A}");
4765795a
JH
302
303 $a =~ s/^A/Z/;
3a2263fe 304 ok(length($a) == 4 && $a eq "ZBC\x{263A}");
4765795a
JH
305}
306
307{
308 my @a = split(/\xFE/, "\xFF\xFE\xFD");
c1a7495a 309 my $b = split(/\xFE/, "\xFF\xFE\xFD");
4765795a 310
3a2263fe 311 ok(@a == 2 && $a[0] eq "\xFF" && $a[1] eq "\xFD");
c1a7495a 312 is($b, scalar(@a));
4765795a
JH
313}
314
16bdb4ac
RG
315{
316 # check that PMf_WHITE is cleared after \s+ is used
317 # reported in <20010627113312.RWGY6087.viemta06@localhost>
318 my $r;
319 foreach my $pat ( qr/\s+/, qr/ll/ ) {
320 $r = join ':' => split($pat, "hello cruel world");
321 }
3a2263fe 322 is($r, "he:o cruel world");
16bdb4ac 323}
6de67870
JP
324
325
326{
327 # split /(A)|B/, "1B2" should return (1, undef, 2)
328 my @x = split /(A)|B/, "1B2";
c1a7495a
BB
329 my $y = split /(A)|B/, "1B2";
330 is($y, scalar(@x));
3a2263fe 331 ok($x[0] eq '1' and (not defined $x[1]) and $x[2] eq '2');
6de67870 332}
1d86a7f9
HS
333
334{
335 # [perl #17064]
336 my $warn;
337 local $SIG{__WARN__} = sub { $warn = join '', @_; chomp $warn };
338 my $char = "\x{10f1ff}";
339 my @a = split /\r?\n/, "$char\n";
c1a7495a
BB
340 my $b = split /\r?\n/, "$char\n";
341 is($b, scalar(@a));
3a2263fe
RGS
342 ok(@a == 1 && $a[0] eq $char && !defined($warn));
343}
344
345{
346 # [perl #18195]
e1c3fb40
RGS
347 for my $u (0, 1) {
348 for my $a (0, 1) {
349 $_ = 'readin,database,readout';
350 utf8::upgrade $_ if $u;
351 /(.+)/;
352 my @d = split /[,]/,$1;
c1a7495a
BB
353 my $e = split /[,]/,$1;
354 is($e, scalar(@d));
e1c3fb40 355 is(join (':',@d), 'readin:database:readout', "[perl #18195]");
3a2263fe 356 }
1d86a7f9
HS
357 }
358}
3b0d546b
AE
359
360{
361 $p="a,b";
362 utf8::upgrade $p;
7f18b612 363 eval { @a=split(/[, ]+/,$p) };
c1a7495a
BB
364 eval { $b=split(/[, ]+/,$p) };
365 is($b, scalar(@a));
3b0d546b
AE
366 is ("$@-@a-", '-a b-', '#20912 - split() to array with /[]+/ and utf8');
367}
7f18b612
YST
368
369{
9ce1a4d5 370 # LATIN SMALL LETTER A WITH DIAERESIS, CYRILLIC SMALL LETTER I
efb52d86 371 for my $pattern ("\N{U+E4}", "\x{0437}") {
9ce1a4d5
RS
372 utf8::upgrade $pattern;
373 my @res;
374 for my $str ("a${pattern}b", "axb", "a${pattern}b") {
375 @split = split /$pattern/, $str;
376 push @res, scalar(@split);
377 }
378 is($res[0], 2);
379 is($res[1], 1);
380 is($res[2], 2, '#123469 - split with utf8 pattern after handling non-utf8 EXPR');
381 }
382}
383
384{
7f18b612
YST
385 is (\@a, \@{"a"}, '@a must be global for following test');
386 $p="";
387 $n = @a = split /,/,$p;
388 is ($n, 0, '#21765 - pmreplroot hack used to return undef for 0 iters');
389}
e3a8873f
DM
390
391{
392 # [perl #28938]
393 # assigning off the end of the array after a split could leave garbage
394 # in the inner elements
395
396 my $x;
397 @a = split /,/, ',,,,,';
398 $a[3]=1;
399 $x = \$a[2];
400 is (ref $x, 'SCALAR', '#28938 - garbage after extend');
401}
ede8ac17
TS
402
403{
404 my $src = "ABC \0 FOO \0 XYZ";
405 my @s = split(" \0 ", $src);
406 my @r = split(/ \0 /, $src);
c1a7495a
BB
407 my $cs = split(" \0 ", $src);
408 my $cr = split(/ \0 /, $src);
ede8ac17 409 is(scalar(@s), 3);
c1a7495a
BB
410 is($cs, 3);
411 is($cr, 3);
ede8ac17
TS
412 is($s[0], "ABC");
413 is($s[1], "FOO");
414 is($s[2]," XYZ");
415 is(join(':',@s), join(':',@r));
416}
b8de32d5
AV
417
418{
419 use constant BANG => {};
420 () = split m/,/, "", BANG;
421 ok(1);
422}
941446f6
FC
423
424{
2f7a9718 425 # Bug #69875
941446f6
FC
426 # 'Hybrid' scalar-and-array context
427 scalar(our @PATH = split /::/, "Font::GlyphNames");
428 # 'my' doesn't trigger the bug
429 is "@PATH", "Font GlyphNames", "hybrid scalar-and-array context";
430}
5255171e 431
cd346b28
JK
432{
433 my @results;
dbc200c5
YO
434 my $expr= "foo bar";
435 my $cond;
436
437 @results= split(0||" ", $expr);
438 is @results, 2, 'split(0||" ") is treated like split(" ")'; #'
439
440 $cond= 0;
441 @results= split $cond ? " " : qr/ /, $expr;
442 is @results, 3, 'split($cond ? " " : qr/ /, $expr) works as expected (like qr/ /)';
443 $cond= 1;
444 @results= split $cond ? " " : qr/ /, $expr;
445 is @results, 2, 'split($cond ? " " : qr/ /, $expr) works as expected (like " ")';
cd346b28 446
dbc200c5 447 $expr = ' a b c ';
cd346b28
JK
448 @results = split /\s/, $expr;
449 is @results, 4,
450 "split on regex of single space metacharacter: captured 4 elements";
451 is $results[0], '',
452 "split on regex of single space metacharacter: first element is empty string";
453
454 @results = split / /, $expr;
455 is @results, 4,
456 "split on regex of single whitespace: captured 4 elements";
457 is $results[0], '',
458 "split on regex of single whitespace: first element is empty string";
459
460 @results = split " ", $expr;
461 is @results, 3,
462 "split on string of single whitespace: captured 3 elements";
463 is $results[0], 'a',
464 "split on string of single whitespace: first element is non-empty";
465
466 $expr = " a \tb c ";
467 @results = split " ", $expr;
468 is @results, 3,
469 "split on string of single whitespace: captured 3 elements";
470 is $results[0], 'a',
471 "split on string of single whitespace: first element is non-empty; multiple contiguous space characters";
dbc200c5
YO
472
473 my @seq;
474 for my $cond (0,1,0,1,0) {
475 $expr = " foo ";
476 @results = split $cond ? qr/ / : " ", $expr;
477 push @seq, scalar(@results) . ":" . $results[-1];
478 }
479 is join(" ", @seq), "1:foo 3:foo 1:foo 3:foo 1:foo",
480 qq{split(\$cond ? qr/ / : " ", "$exp") behaves as expected over repeated similar patterns};
cd346b28
JK
481}
482
dbc200c5
YO
483{
484 # 'RT #116086: split "\x20" does not work as documented';
cd346b28
JK
485 my @results;
486 my $expr;
487 $expr = ' a b c ';
74f6d97d 488 @results = split uni_to_native("\x20"), $expr;
cd346b28
JK
489 is @results, 3,
490 "RT #116086: split on string of single hex-20: captured 3 elements";
491 is $results[0], 'a',
492 "RT #116086: split on string of single hex-20: first element is non-empty";
493
494 $expr = " a \tb c ";
74f6d97d 495 @results = split uni_to_native("\x20"), $expr;
cd346b28
JK
496 is @results, 3,
497 "RT #116086: split on string of single hex-20: captured 3 elements";
498 is $results[0], 'a',
499 "RT #116086: split on string of single hex-20: first element is non-empty; multiple contiguous space characters";
500}
501
60041a09
FC
502# Nasty interaction between split and use constant
503use constant nought => 0;
504($a,$b,$c) = split //, $foo, nought;
505is nought, 0, 'split does not mangle 0 constants';
821956c5
FC
506
507*aaa = *bbb;
508$aaa[1] = "foobarbaz";
509$aaa[1] .= "";
510@aaa = split //, $bbb[1];
511is "@aaa", "f o o b a r b a z",
512 'split-to-array does not free its own argument';
afc80078
FC
513
514() = @a = split //, "abc";
515is "@a", "a b c", '() = split-to-array';
e4e95921
FC
516
517(@a = split //, "abc") = 1..10;
ebe6eeaa
FC
518is "@a", '1 2 3', 'assignment to split-to-array (pmtarget/package array)';
519{
520 my @a;
521 (@a = split //, "abc") = 1..10;
522 is "@a", '1 2 3', 'assignment to split-to-array (targ/lexical)';
523}
524(@{\@a} = split //, "abc") = 1..10;
525is "@a", '1 2 3', 'assignment to split-to-array (stacked)';
5012eebe
DM
526
527# check that re-evals work
528
529{
530 my $c = 0;
531 @a = split /-(?{ $c++ })/, "a-b-c";
532 is "@a", "a b c", "compile-time re-eval";
533 is $c, 2, "compile-time re-eval count";
534
535 my $sep = '-';
536 $c = 0;
537 @a = split /$sep(?{ $c++ })/, "a-b-c";
538 is "@a", "a b c", "run-time re-eval";
539 is $c, 2, "run-time re-eval count";
540}
692044df
DM
541
542# check that that my/local @array = split works
543
544{
545 my $s = "a:b:c";
546
547 local @a = qw(x y z);
548 {
549 local @a = split /:/, $s;
550 is "@a", "a b c", "local split inside";
551 }
552 is "@a", "x y z", "local split outside";
553
554 my @b = qw(x y z);
555 {
556 my @b = split /:/, $s;
557 is "@b", "a b c", "my split inside";
558 }
559 is "@b", "x y z", "my split outside";
560}
561
562# check that the (@a = split) optimisation works in scalar/list context
563
564{
565 my $s = "a:b:c:d:e";
566 my @outer;
567 my $outer;
568 my @lex;
569 local our @pkg;
570
571 $outer = (@lex = split /:/, $s);
572 is "@lex", "a b c d e", "array split: scalar cx lex: inner";
573 is $outer, 5, "array split: scalar cx lex: outer";
574
575 @outer = (@lex = split /:/, $s);
576 is "@lex", "a b c d e", "array split: list cx lex: inner";
577 is "@outer", "a b c d e", "array split: list cx lex: outer";
578
579 $outer = (@pkg = split /:/, $s);
580 is "@pkg", "a b c d e", "array split: scalar cx pkg inner";
581 is $outer, 5, "array split: scalar cx pkg outer";
582
583 @outer = (@pkg = split /:/, $s);
584 is "@pkg", "a b c d e", "array split: list cx pkg inner";
585 is "@outer", "a b c d e", "array split: list cx pkg outer";
586
587 $outer = (my @a1 = split /:/, $s);
588 is "@a1", "a b c d e", "array split: scalar cx my lex: inner";
589 is $outer, 5, "array split: scalar cx my lex: outer";
590
591 @outer = (my @a2 = split /:/, $s);
592 is "@a2", "a b c d e", "array split: list cx my lex: inner";
593 is "@outer", "a b c d e", "array split: list cx my lex: outer";
594
595 $outer = (local @pkg = split /:/, $s);
596 is "@pkg", "a b c d e", "array split: scalar cx local pkg inner";
597 is $outer, 5, "array split: scalar cx local pkg outer";
598
599 @outer = (local @pkg = split /:/, $s);
600 is "@pkg", "a b c d e", "array split: list cx local pkg inner";
601 is "@outer", "a b c d e", "array split: list cx local pkg outer";
602
603 $outer = (@{\@lex} = split /:/, $s);
604 is "@lex", "a b c d e", "array split: scalar cx lexref inner";
605 is $outer, 5, "array split: scalar cx lexref outer";
606
607 @outer = (@{\@pkg} = split /:/, $s);
608 is "@pkg", "a b c d e", "array split: list cx pkgref inner";
609 is "@outer", "a b c d e", "array split: list cx pkgref outer";
610
611
612}