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

Re: [PATCH] Remove implicit split to @_

Thread Previous | Thread Next
From:
Michael G Schwern
Date:
July 10, 2009 13:49
Subject:
Re: [PATCH] Remove implicit split to @_
Message ID:
4A57A958.5010307@pobox.com
Chip Salzenberg wrote:
> This is an amusing discussion.
> 
> The only problem that I observe with scalar split writing to @_ implicitly
> is that it's an easy thing to do by accident and is almost never useful.
> Therefore, removing it seems a public service.
> 
> I favor simply making it a compile-time error to use split in scalar
> context.  No problem with silently changing semantics and no more surprising
> behavior.  Everybody's happy.  (right?)

Except for all the newbies who ask "but why can't I do that?" and then we go
into a sheepish explanation about how p5p didn't think its useful and so they
denied it in a language that's supposed to be TMTOWTDI.

I mean, its TMTOWTDI and if the WTDI in your brain is to see the parallel with
split() and "arrays return their length in scalar context".  I know perlfunc
has the weasel statement about how there's no rule relating list vs scalar
context, but is there a good reason to make that MORE inconsistent?

Making it a compile-time error isn't compromise, its picking the lowest common
denominator.

People will not be surprised that C<<my $count = split ...;>> works.  You
might not be sure the first time you use it what it will do, but I guarantee
there will be no confusion the second time.  And that's good UI design.

In a language full of surprises what we need is one less surprise.

Its more work to remove it, its LESS compatible with the part of split() in
scalar that people might actually be using, it introduces a hole in the
interface and IF someone might want to do this thing its the most obvious way
to do it (assuming you're not a Perl guru).  Easy things easy.  Hard things
possible.

It also means we can optimize it to just count the fields and not store an
array.  And everyone loves optimizations!


Yitzchak said:
> Apparently it's something Schwern has wanted for some time, and since
> he has gone to the trouble of actually writing a patch, his voice has
> a good deal of weight behind it.

Thank you.  Its something that comes up every once in a while, the warning
that is, and every time I have to look to see what the hell it means.

This particular case came up because my friend is learning Perl, she got that
warning, I began to explain how to work around it, got embarrassed and just
fixed it.

Also, as others have demonstrated trying to come up with alternatives and
found talking with her today, there is no really obvious way to do it.

    Kelli Ireland
    So why would you use $count = split(/ /, $thing); instead of
    $count = count(" ", $thing); (or whatever the right perl fu is)

    Kelli Ireland
    Er, +1.

[My note:  That forgetting the +1 demonstrates a trap with all the proposed
alternative techniques.  You're counting separators, not fields, making an off
by one error really easy]

    Michael Schwern
    Heh, there is no right Perl fu for this
    Every other way to do it is complicated.

    Kelli Ireland
    What?

    Michael Schwern
    Try it

    Kelli Ireland
    You can't easily count the number of "a" in a string?
    Or spaces?

    Michael Schwern
    You can, as long as its not a regex.
    tr///

    Kelli Ireland
    ?

    Michael Schwern
    tr/// is the transliteration operator
    tr/a/b/ will turn all a into b
    You can press it into service to count characters.
    But it doesn't take a regex

    Michael Schwern
    Here's the easiest way I can think to do it.
    my $string = "foo\tbar\tbaz";
    my $count = () = $string =~ m/\t/g;
    $count++;
    Which contains a bunch of magic.

I hate having conversations like that.  "This easy thing?  Its not easy."

Kelli came up with some simple use cases.

    Kelli Ireland
    I don't want to know the list of students in my class,
    I just want a count so that I can print out the correct
    number of handouts.

    I don't need to know the actual names of intersections that
    I pass before I get to the store, I just need to count them.
    It's faster and takes less memory.


Yitzchak also said:
> IMO, experience has shown that kind of action at a distance in language
> design to be a Bad Thing(TM).

+3.14


-- 
Insulting our readers is part of our business model.
        http://somethingpositive.net/sp07122005.shtml

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