Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Use of wantarray Considered Harmful

by dragonchild (Archbishop)
on Dec 13, 2008 at 17:33 UTC ( #730174=note: print w/ replies, xml ) Need Help??


in reply to Use of wantarray Considered Harmful

First off, Contextual::Return is a great idea and only makes the problem you're describing worse. I've tried it several times and cannot do anything but ignore it.

Second, there is really only one good use of wantarray - to return an array or an iterator. Array vs. arrayref is tolerable, but only barely. Array vs. first element is completely useless. The key here is that even though different context is used, the same thing is being returned. If you do array vs. first element, you're changing the semantics of the function - it does something different. Arrays vs. iterators are the same thing. Arrays vs. arrayrefs suck because of Perl's syntactic differentiation between arrays/hashes and references to them and the difficulty in determining scalar type. But, given that "scalar" is a type and the various reftype() values are more subtypes than first-class types, this isn't surprising. This, actually, is one of the things I'm looking forward to Perl6 the most - actual proper typing.


My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?


Comment on Re: Use of wantarray Considered Harmful
Re^2: Use of wantarray Considered Harmful
by BrowserUk (Pope) on Dec 13, 2008 at 20:22 UTC
      Whether or not caller and each behave sanely is a separate question. Just because it's in core doesn't mean it's a good idea. Frankly, I think that a subroutine should return one and only one type of entity. It shouldn't return a collection (array, in this case) in one situation and an element (first element, in this case) in another. That leads to serious hard-to-debug issues. This isn't theoretical - it's personal experience speaking.

      My criteria for good software:
      1. Does it work?
      2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
        Frankly, I think that a subroutine should return one and only one type of entity.... That leads to serious hard-to-debug issues. This isn't theoretical - it's personal experience speaking.

        In isolation, those sound like wise words, but essentially what you are saying now, is that you think all uses of Perl's context sensitivity are bad and should be deprecated, because you've had trouble with it occasionally. That argument is as fallacious as these, please read the counter arguments.

        Context is a unique, integral, important and powerful feature of Perl--and unless things have changed markedly in the last short while--it will be at the core of Perl 6 also.

        Speaking Perl without using context, is like speaking English (or French or Dutch or German), without contractions. Possible, but stilted, unnatural, laboured. It's speaking Perl like a foriegner.

        Children are using contractions correctly aged 3 or 4 years. Can it really be so hard to get right?

        Please, please. Is there no one out there with political weight and a descent level of (natural) language skills that'll help me defend my beloved Perl from the one-idiom-fits-all, OO uniformatists?


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
        Frankly, I think that a subroutine should return one and only one type of entity. It shouldn't return a collection (array, in this case) in one situation and an element (first element, in this case) in another.

        But by virtue of how context works in Perl, this issue is actually beyond the control of the subroutine's designer/coder. A subroutine that returns a list will, by virtue of how context works, return a scalar in scalar context (as per ikegami's table above). If some problem is "hard-to-debug" on this basis, it is because the person doing the debugging doesn't understand context -- a situation that is all too frequent among people using Perl, obviously.

        Given this environment within which the subroutine designer/coder must work, the key point, as explained by BrowserUK and some others in this thread, is to document, clearly and accurately, what the subroutine does in different contexts, even if this amounts to repeating (briefly) the standard description of Perl's normal behavior (because the next "maintainer" might be some novice who will be seeing this information for the first time). Anyone who tries to use (or debug the use of) someone else's subroutine without reading the docs for it will get no sympathy or special indulgence from me.

        As long as context-dependent behavior is properly documented, I don't see a problem with it, and in fact I appreciate the flexibility that it makes available.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://730174]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2014-11-28 03:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (192 votes), past polls