| Store | Cart

Re: Context broken in split?

From: Abigail <abig...@abigail.be>
Thu, 23 Oct 2014 16:02:07 +0200
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

Recent Messages in this Thread
demerphq Oct 23, 2014 11:42 am
Eirik Berg Hanssen Oct 23, 2014 12:13 pm
Mark Overmeer Oct 23, 2014 01:02 pm
Ricardo Signes Oct 23, 2014 01:13 pm
Abigail Oct 23, 2014 02:02 pm
Eirik Berg Hanssen Oct 23, 2014 03:33 pm
Mark Overmeer Oct 23, 2014 02:26 pm
Ricardo Signes Oct 23, 2014 02:47 pm
demerphq Oct 23, 2014 03:02 pm
Father Chrysostomos Oct 23, 2014 03:33 pm
Messages in this thread