Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^4: Use of wantarray Considered Harmful

by graff (Chancellor)
on Dec 14, 2008 at 04:00 UTC ( #730242=note: print w/ replies, xml ) Need Help??


in reply to Re^3: Use of wantarray Considered Harmful
in thread Use of wantarray Considered Harmful

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.


Comment on Re^4: Use of wantarray Considered Harmful

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2014-12-20 18:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (97 votes), past polls