develooper Front page | perl.perl5.porters | Postings from July 2009

Re: [PATCH] Remove implicit split to @_

Thread Previous | Thread Next
From:
Aaron Sherman
Date:
July 13, 2009 15:18
Subject:
Re: [PATCH] Remove implicit split to @_
Message ID:
eaa2c6270907131517r3dd05a13m870cd7db3505805c@mail.gmail.com
On Mon, Jul 13, 2009 at 4:16 PM, Eric Brine <ikegami@adaelis.com> wrote:

> On Fri, Jul 10, 2009 at 7:55 PM, Yitzchak Scott-Thoennes
> <sthoenna@efn.org>wrote:
>
> > On Fri, July 10, 2009 11:12 am, Eric Brine wrote:
> > > On Fri, Jul 10, 2009 at 1:45 PM, Yitzchak Scott-Thoennes wrote:
> > >> $count = () = split(/PATTERN/, EXPR) doesn't work, but
> > >> $count = () = split(/PATTERN/, EXPR, -1) does.
> > >
> > > Could this be added to the docs, please?
> >
> > Already there, http://perldoc.perl.org/functions/split.html:
>
> ...
>
> > although I'm not quite certain what the "this" you meant is.
>
>
> An example of how to count fields using split. That's heavy reading to find
> out a common idiom doesn't work with split. I believe it's singular in this
> optimisation.
>

Agreed, it seems quite odd to me, and I've been using Perl since just after
4.0 landed. There's an expectation in Perl that lists in a scalar context
have a measure of DWIMery. In the case of split, that's just not true.

Now, if this were Java or Python or even Perl 6, I'd say that's fine. Those
languages have methods that can be called on the resulting containers to
count their members. Perl quite simply doesn't.

 warn "Not enough fields" unless split(//, $_, 5) == 5

has a really nice Perl 5 feel to it, and I don't think it's asking for a
massive redesign to say that it should go on the list for a future release.


The counter argument is that you almost always want to say:

  @fields = split(//, $_, 5);
  warn "Not enough fields" unless @fields == 5;
  ... do something with @fields ...

I can see the logic, there, but I'm not sure I buy its ultimate value. We're
arguing down value in terms of meeting user expectations by claiming that
consistency won't typically be required. If we're saying that it's too much
work or that it break too much code, I can see that, but if we're just
arguing that consistency (in that strange and lovable way that Perl's
internally consistent) isn't necessary for usability in lesser-used nooks of
the language, then I'm going to disagree.... In fact, I'd suggest that
that's why split in a scalar context has had such an unexpected behavior for
so long.

For further exploration of the problems with rare exceptions, I point you
to:

http://thewhereblog.blogspot.com/2008/12/paradox-of-false-positive.html

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