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