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

Re: Context broken in split?

Thread Previous | Thread Next
From:
Ricardo Signes
Date:
May 26, 2015 12:33
Subject:
Re: Context broken in split?
Message ID:
20150526123332.GC15129@cancer.codesimply.com
* Father Chrysostomos <sprout@cpan.org> [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
perlfunc:

> 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
documentation.

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

-- 
rjbs

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About