15 ($a,$b,$c) = split($FS,$_);
17 is(join(';',$a,$b,$c), 'a;b;c');
21 is(join("$_",@ary), 'aa:b:cc');
22 is($cnt, scalar(@ary));
25 my @xyz = (@ary = split(//));
27 is(join(".",@ary), "a.b.c.\n");
28 is($cnt, scalar(@ary));
33 is(join(".",@ary), "a.b.c");
34 is($cnt, scalar(@ary));
36 $_ = join(':',split(' '," a b\tc \t d "));
38 @ary = split(' '," a b\tc \t d ");
39 $cnt = split(' '," a b\tc \t d ");
40 is($cnt, scalar(@ary));
42 $_ = join(':',split(/ */,"foo bar bie\tdoll"));
43 is($_ , "f:o:o:b:a:r:b:i:e:\t:d:o:l:l");
44 @ary = split(/ */,"foo bar bie\tdoll");
45 $cnt = split(/ */,"foo bar bie\tdoll");
46 is($cnt, scalar(@ary));
48 $_ = join(':', 'foo', split(/ /,'a b c'), 'bar');
49 is($_, "foo:a:b::c:bar");
50 @ary = split(/ /,'a b c');
51 $cnt = split(/ /,'a b c');
52 is($cnt, scalar(@ary));
54 # Can we say how many fields to split to?
55 $_ = join(':', split(' ','1 2 3 4 5 6', 3));
56 is($_, '1:2:3 4 5 6');
57 @ary = split(' ','1 2 3 4 5 6', 3);
58 $cnt = split(' ','1 2 3 4 5 6', 3);
59 is($cnt, scalar(@ary));
61 # Can we do it as a variable?
63 $_ = join(':', split(' ','1 2 3 4 5 6', $x));
64 is($_, '1:2:3:4 5 6');
65 @ary = split(' ','1 2 3 4 5 6', $x);
66 $cnt = split(' ','1 2 3 4 5 6', $x);
67 is($cnt, scalar(@ary));
69 # Does the 999 suppress null field chopping?
70 $_ = join(':', split(/:/,'1:2:3:4:5:6:::', 999));
71 is($_ , '1:2:3:4:5:6:::');
72 @ary = split(/:/,'1:2:3:4:5:6:::', 999);
73 $cnt = split(/:/,'1:2:3:4:5:6:::', 999);
74 is($cnt, scalar(@ary));
76 # Does assignment to a list imply split to one more field than that?
77 $foo = runperl( switches => ['-Dt'], stderr => 1, prog => '($a,$b)=split;' );
78 ok($foo =~ /DEBUGGING/ || $foo =~ /const\n?\Q(IV(3))\E/);
80 # Can we say how many fields to split to when assigning to a list?
81 ($a,$b) = split(' ','1 2 3 4 5 6', 2);
83 is($_, '1:2 3 4 5 6');
85 # do subpatterns generate additional fields (without trailing nulls)?
86 $_ = join '|', split(/,|(-)/, "1-10,20,,,");
88 @ary = split(/,|(-)/, "1-10,20,,,");
89 $cnt = split(/,|(-)/, "1-10,20,,,");
90 is($cnt, scalar(@ary));
92 # do subpatterns generate additional fields (with a limit)?
93 $_ = join '|', split(/,|(-)/, "1-10,20,,,", 10);
94 is($_, "1|-|10||20||||||");
95 @ary = split(/,|(-)/, "1-10,20,,,", 10);
96 $cnt = split(/,|(-)/, "1-10,20,,,", 10);
97 is($cnt, scalar(@ary));
99 # is the 'two undefs' bug fixed?
100 (undef, $a, undef, $b) = qw(1 2 3 4);
103 # .. even for locals?
105 local(undef, $a, undef, $b) = qw(1 2 3 4);
109 # check splitting of null string
110 $_ = join('|', split(/x/, '',-1), 'Z');
112 @ary = split(/x/, '',-1);
113 $cnt = split(/x/, '',-1);
114 is($cnt, scalar(@ary));
116 $_ = join('|', split(/x/, '', 1), 'Z');
118 @ary = split(/x/, '', 1);
119 $cnt = split(/x/, '', 1);
120 is($cnt, scalar(@ary));
122 $_ = join('|', split(/(p+)/,'',-1), 'Z');
124 @ary = split(/(p+)/,'',-1);
125 $cnt = split(/(p+)/,'',-1);
126 is($cnt, scalar(@ary));
128 $_ = join('|', split(/.?/, '',-1), 'Z');
130 @ary = split(/.?/, '',-1);
131 $cnt = split(/.?/, '',-1);
132 is($cnt, scalar(@ary));
135 # Are /^/m patterns scanned?
136 $_ = join '|', split(/^a/m, "a b a\na d a", 20);
137 is($_, "| b a\n| d a");
138 @ary = split(/^a/m, "a b a\na d a", 20);
139 $cnt = split(/^a/m, "a b a\na d a", 20);
140 is($cnt, scalar(@ary));
142 # Are /$/m patterns scanned?
143 $_ = join '|', split(/a$/m, "a b a\na d a", 20);
144 is($_, "a b |\na d |");
145 @ary = split(/a$/m, "a b a\na d a", 20);
146 $cnt = split(/a$/m, "a b a\na d a", 20);
147 is($cnt, scalar(@ary));
149 # Are /^/m patterns scanned?
150 $_ = join '|', split(/^aa/m, "aa b aa\naa d aa", 20);
151 is($_, "| b aa\n| d aa");
152 @ary = split(/^aa/m, "aa b aa\naa d aa", 20);
153 $cnt = split(/^aa/m, "aa b aa\naa d aa", 20);
154 is($cnt, scalar(@ary));
156 # Are /$/m patterns scanned?
157 $_ = join '|', split(/aa$/m, "aa b aa\naa d aa", 20);
158 is($_, "aa b |\naa d |");
159 @ary = split(/aa$/m, "aa b aa\naa d aa", 20);
160 $cnt = split(/aa$/m, "aa b aa\naa d aa", 20);
161 is($cnt, scalar(@ary));
165 @ary = split(/\s*:\s*/);
166 $cnt = split(/\s*:\s*/);
167 is(($res = join(".",@ary)), "a.b.c.d", $res);
168 is($cnt, scalar(@ary));
170 # use of match result as pattern (!)
171 is('p:q:r:s', join ':', split('abc' =~ /b/, 'p1q1r1s'));
172 @ary = split('abc' =~ /b/, 'p1q1r1s');
173 $cnt = split('abc' =~ /b/, 'p1q1r1s');
174 is($cnt, scalar(@ary));
176 # /^/ treated as /^/m
177 $_ = join ':', split /^/, "ab\ncd\nef\n";
178 is($_, "ab\n:cd\n:ef\n");
180 # see if @a = @b = split(...) optimization works
181 @list1 = @list2 = split ('p',"a p b c p");
182 ok(@list1 == @list2 &&
183 "@list1" eq "@list2" &&
185 "@list1" eq "a b c ");
187 # zero-width assertion
188 $_ = join ':', split /(?=\w)/, "rm b";
190 @ary = split /(?=\w)/, "rm b";
191 $cnt = split /(?=\w)/, "rm b";
192 is($cnt, scalar(@ary));
196 @ary = map {ord} split //, v1.20.300.4000.50000.4000.300.20.1;
197 $cnt = split //, v1.20.300.4000.50000.4000.300.20.1;
198 is("@ary", "1 20 300 4000 50000 4000 300 20 1");
199 is($cnt, scalar(@ary));
201 @ary = split(/\x{FE}/, "\x{FF}\x{FE}\x{FD}"); # bug id 20010105.016
202 $cnt = split(/\x{FE}/, "\x{FF}\x{FE}\x{FD}"); # bug id 20010105.016
204 $ary[0] eq "\xFF" && $ary[1] eq "\xFD" &&
205 $ary[0] eq "\x{FF}" && $ary[1] eq "\x{FD}");
206 is($cnt, scalar(@ary));
208 @ary = split(/(\x{FE}\xFE)/, "\xFF\x{FF}\xFE\x{FE}\xFD\x{FD}"); # variant of 31
209 $cnt = split(/(\x{FE}\xFE)/, "\xFF\x{FF}\xFE\x{FE}\xFD\x{FD}"); # variant of 31
211 $ary[0] eq "\xFF\xFF" &&
212 $ary[0] eq "\x{FF}\xFF" &&
213 $ary[0] eq "\x{FF}\x{FF}" &&
214 $ary[1] eq "\xFE\xFE" &&
215 $ary[1] eq "\x{FE}\xFE" &&
216 $ary[1] eq "\x{FE}\x{FE}" &&
217 $ary[2] eq "\xFD\xFD" &&
218 $ary[2] eq "\x{FD}\xFD" &&
219 $ary[2] eq "\x{FD}\x{FD}");
220 is($cnt, scalar(@ary));
223 my @a = map ord, split(//, join("", map chr, (1234, 123, 2345)));
224 my $c = split(//, join("", map chr, (1234, 123, 2345)));
225 is("@a", "1234 123 2345");
231 my @a = map ord, split(/$x/, join("", map chr, (1234, ord($x), 2345)));
232 my $c = split(/$x/, join("", map chr, (1234, ord($x), 2345)));
233 is("@a", "1234 2345");
238 # bug id 20000427.003
243 my $sushi = "\x{b36c}\x{5a8c}\x{ff5b}\x{5079}\x{505b}";
245 my @charlist = split //, $sushi;
246 my $charnum = split //, $sushi;
247 is($charnum, scalar(@charlist));
249 foreach my $ch (@charlist) {
250 $r = $r . " " . sprintf "U+%04X", ord($ch);
253 is($r, " U+B36C U+5A8C U+FF5B U+5079 U+505B");
257 my $s = "\x20\x40\x{80}\x{100}\x{80}\x40\x20";
260 if (ord('A') == 193) {
263 # bug id 20000426.003
265 my ($a, $b, $c) = split(/\x40/, $s);
266 ok($a eq "\x20" && $b eq "\x{80}\x{100}\x{80}" && $c eq $a);
270 my ($a, $b) = split(/\x{100}/, $s);
271 ok($a eq "\x20\x40\x{80}" && $b eq "\x{80}\x40\x20");
273 my ($a, $b) = split(/\x{80}\x{100}\x{80}/, $s);
274 ok($a eq "\x20\x40" && $b eq "\x40\x20");
277 if (ord('A') == 193) {
280 my ($a, $b) = split(/\x40\x{80}/, $s);
281 ok($a eq "\x20" && $b eq "\x{100}\x{80}\x40\x20");
285 my ($a, $b, $c) = split(/[\x40\x{80}]+/, $s);
286 ok($a eq "\x20" && $b eq "\x{100}" && $c eq "\x20");
292 my $a = "ABC\x{263A}";
294 my @b = split( //, $a );
295 my $c = split( //, $a );
300 ok(length($b[3]) == 1 && $b[3] eq "\x{263A}");
303 ok(length($a) == 4 && $a eq "ZBC\x{263A}");
307 my @a = split(/\xFE/, "\xFF\xFE\xFD");
308 my $b = split(/\xFE/, "\xFF\xFE\xFD");
310 ok(@a == 2 && $a[0] eq "\xFF" && $a[1] eq "\xFD");
315 # check that PMf_WHITE is cleared after \s+ is used
316 # reported in <20010627113312.RWGY6087.viemta06@localhost>
318 foreach my $pat ( qr/\s+/, qr/ll/ ) {
319 $r = join ':' => split($pat, "hello cruel world");
321 is($r, "he:o cruel world");
326 # split /(A)|B/, "1B2" should return (1, undef, 2)
327 my @x = split /(A)|B/, "1B2";
328 my $y = split /(A)|B/, "1B2";
330 ok($x[0] eq '1' and (not defined $x[1]) and $x[2] eq '2');
336 local $SIG{__WARN__} = sub { $warn = join '', @_; chomp $warn };
337 my $char = "\x{10f1ff}";
338 my @a = split /\r?\n/, "$char\n";
339 my $b = split /\r?\n/, "$char\n";
341 ok(@a == 1 && $a[0] eq $char && !defined($warn));
348 $_ = 'readin,database,readout';
349 utf8::upgrade $_ if $u;
351 my @d = split /[,]/,$1;
352 my $e = split /[,]/,$1;
354 is(join (':',@d), 'readin:database:readout', "[perl #18195]");
362 eval { @a=split(/[, ]+/,$p) };
363 eval { $b=split(/[, ]+/,$p) };
365 is ("$@-@a-", '-a b-', '#20912 - split() to array with /[]+/ and utf8');
369 is (\@a, \@{"a"}, '@a must be global for following test');
371 $n = @a = split /,/,$p;
372 is ($n, 0, '#21765 - pmreplroot hack used to return undef for 0 iters');
377 # assigning off the end of the array after a split could leave garbage
378 # in the inner elements
381 @a = split /,/, ',,,,,';
384 is (ref $x, 'SCALAR', '#28938 - garbage after extend');
387 # check the special casing of split /\s/ and unicode
388 use charnames qw(:full);
389 # below test data is extracted from
391 # Date: 2006-06-07, 23:22:52 GMT [MD]
393 # Unicode Character Database
394 # Copyright (c) 1991-2006 Unicode, Inc.
395 # For terms of use, see http://www.unicode.org/terms_of_use.html
396 # For documentation, see UCD.html
398 ord("\t"), # Cc <control-0009>
399 ord("\n"), # Cc <control-000A>
400 # not PerlSpace # Cc <control-000B>
401 ord("\f"), # Cc <control-000C>
402 ord("\r"), # Cc <control-000D>
404 ord("\N{NEL}"), # Cc <control-0085>
405 ord("\N{NO-BREAK SPACE}"),
407 0x1680, # Zs OGHAM SPACE MARK
408 0x180E, # Zs MONGOLIAN VOWEL SEPARATOR
409 0x2000..0x200A, # Zs [11] EN QUAD..HAIR SPACE
410 0x2028, # Zl LINE SEPARATOR
411 0x2029, # Zp PARAGRAPH SEPARATOR
412 0x202F, # Zs NARROW NO-BREAK SPACE
413 0x205F, # Zs MEDIUM MATHEMATICAL SPACE
414 0x3000 # Zs IDEOGRAPHIC SPACE
416 #diag "Have @{[0+@spaces]} to test\n";
417 foreach my $cp (@spaces) {
418 my $msg = sprintf "Space: U+%04x", $cp;
419 my $space = chr($cp);
420 my $str="A:$space:B\x{FFFD}";
423 my @res=split(/\s+/,$str);
424 my $cnt=split(/\s+/,$str);
425 ok(@res == 2 && join('-',@res) eq "A:-:B", "$msg - /\\s+/");
426 is($cnt, scalar(@res), "$msg - /\\s+/ (count)");
428 my $s2 = "$space$space:A:$space$space:B\x{FFFD}";
431 my @r2 = split(' ',$s2);
432 my $c2 = split(' ',$s2);
433 ok(@r2 == 2 && join('-', @r2) eq ":A:-:B", "$msg - ' '");
434 is($c2, scalar(@r2), "$msg - ' ' (count)");
436 my @r3 = split(/\s+/, $s2);
437 my $c3 = split(/\s+/, $s2);
438 ok(@r3 == 3 && join('-', @r3) eq "-:A:-:B", "$msg - /\\s+/ No.2");
439 is($c3, scalar(@r3), "$msg - /\\s+/ No.2 (count)");
444 my $src = "ABC \0 FOO \0 XYZ";
445 my @s = split(" \0 ", $src);
446 my @r = split(/ \0 /, $src);
447 my $cs = split(" \0 ", $src);
448 my $cr = split(/ \0 /, $src);
455 is(join(':',@s), join(':',@r));
459 use constant BANG => {};
460 () = split m/,/, "", BANG;