in reply to Re^5: Use of wantarray Considered Harmful
in thread Use of wantarray Considered Harmful
Surprise, surprise! ...there is really only one good use of wantarray - to return an array or an iterator.
Hm. I agree that is a very good use case. And by itself, it justifies having context sensitivity. But once you have context sensitivity, it is certainly not the only good use case.
Perhaps my most often used built-in examples are localtime; m//g; readline (often as not as the diamond operator). These return a scalar or list.
Another contention above is: "a subroutine should return one and only one type of entity.". Not only does the "iterator or array" assertion flatly contradict this--an iterator is either a scalar or a coderef; but certainly not an array--but from another view point, a subroutine can either return a list, or a scalar (or nothing). And a scalar is just a single element list. And nothing is the empty list.
That makes the return always of one type: a list of 0, 1, or many elements. Sounds pretty CS to me.
Context only becomes a problem when you stop thinking in Perl and start thinking in terms of some other language, or overarching paradymn. Just as Great Circle math becomes a problem if you are a flat earther.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^7: Use of wantarray Considered Harmful
by dragonchild (Archbishop) on Dec 19, 2008 at 16:01 UTC | |
by ikegami (Patriarch) on Dec 19, 2008 at 23:04 UTC | |
by BrowserUk (Patriarch) on Dec 19, 2008 at 22:48 UTC |