http://www.perlmonks.org?node_id=962400


in reply to Odd workings of split

Your question has already been answered, but I'd like to point out that this:

$cmd[$_] = [split "\n", $cmd[$_]]->[0];

Might be better as:

$cmd[$_] = [split "\n", $cmd[$_], 2]->[0];

Setting the third parameter to "2" indicates that you don't want split to return any more than 2 items. So if your string is, say, 8 lines long, split will return a list containing two strings: the first line, then all the other lines. This saves Perl a bit of work splitting up a portion of the string that you're uninterested in.

Alternatively, if you use list assignment, like this:

($cmd[$_]) = split "\n", $cmd[$_];

... then Perl is actually smart enough to figure out the ",2" itself. Take a peek at:

perl -MO=Deparse -e'($cmd[$_]) = split "\n", $cmd[$_];'
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'