On Thu, Oct 23, 2014 at 09:13:01AM -0400, Ricardo Signes wrote:
> * Mark Overmeer <solu...@overmeer.net> [2014-10-23T09:02:42]> > I do not read the same in that pod as you do. It speaks about LIMIT,> > not about return values.> > But earlier, the Pod says:> > Splits the string EXPR into a list of strings and returns the list> in list context, or the size of the list in scalar context.> > So if the optimization decides to split with limit 1 on (), which is what it> says it will do, then it will have produced a list with size 1 and return that> in scalar context.> > While this behavior falls out of the documentation, I'm not sure that changing> it would be the end of the world. That said, I'm not sure the behavior itself> is the end of the world...
No dobut the current behaviour is a result of an optimization. I guess
there's a lot to win on:
my ($foo, $bar) = split /COMPLEX_PATTERN/ => $gigantic_string;
Do note though that giving -1 as a third argument is *NOT* just bypassing
this optimization, it does more:
$ perl -wE 'my $x = () = split /,/ => ",,foo,,"; say $x'
1
$ perl -wE 'my $x = (my @x) = split /,/ => ",,foo,,"; say $x'
3
$ perl -wE 'my $x = () = split /,/ => ",,foo,,", -1; say $x'
5
$
Using -1 not only forces split() to split the entire string, it also
prevents it from discarding trailing empty strings. But everyone knew
that, right? ;-)
Abigail