develooper Front page | perl.perl5.porters | Postings from May 2015

Re: Context broken in split?

Thread Previous | Thread Next
Ricardo Signes
May 26, 2015 12:33
Re: Context broken in split?
Message ID:
* Father Chrysostomos <> [2014-10-25T14:20:14]
> This is what it boils down to:
> ($a,$b,@c) = split ',', 'a,';
> print defined $b ? qq'"$b"' : 'undef', "\n";
> ($a,$b) = split ',', 'a,';
> print defined $b ? qq'"$b"' : 'undef', "\n";
> The output is:
> undef
> ""
> But everybody knew that.  Can we change this and risk breakage?  I'd
> like to go ahead and make this into a pure optimisation, but others
> have qualms about it.  After all, the way the optimisation works is
> actually documented.

I find the documentation really unhelpful.  A reading from the book of

> If LIMIT is omitted (or, equivalently, zero), then it is usually
> treated as if it were instead negative but with the exception
> that trailing empty fields are stripped (empty leading fields
> are always preserved); if all fields are empty, then all fields
> are considered to be trailing (and are thus stripped in this
> case).
> ⋮
> In time-critical applications, it is worthwhile to avoid
> splitting into more fields than necessary. Thus, when assigning
> to a list, if LIMIT is omitted (or zero), then LIMIT is treated
> as though it were one larger than the number of variables in the
> list; for the following, LIMIT is implicitly 3.

So, given this code:

  ($a,$b,@c) = split ',', 'a,';

$b ends up undefined.  The second behavior is not evident, even though we are
assigning to a list.

Split is hairy.  I think it's a bit too clever, and I think that people are
going to be relying on its weirdness.  Probably we're best off fixing the

Apart from simplifying split's behaviors, is there some other benefit?  (As
opposed, say, to documenting its behaviors properly.)


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About