A tiny correction: split doesn't return undef, it returns the empty list. Accessing [ (empty list) ]->[0] is what produces the undef.
The documentation of split also mentions this case:
Note that splitting an EXPR that evaluates to the empty string always returns the empty list, regardless of the LIMIT specified.
The problem is really that split is used for too many things, and it tries to cater to all needs.
A different way to approach this is not to ask what to split on, but ask what you want to extract. For example you can write
$cmd[0] = ($cmd[0] =~ /.*/g)[0]
where .* matches every character up to (but excluding) the first newline.
Or explicitly state what you want removed:
$cmd[0] =~ s/\n.*/s;
In Perl 6, split is less magical, and you are encouraged to write patterns that match what you're after (and not patterns that match the separator, so that still works):
$cmd[0].=comb(/\N*/);
I'm not sure what else to write, since you didn't really have a question :-)
In reply to Re: Odd workings of split
by moritz
in thread Odd workings of split
by Ralesk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |