Pathologically Eclectic Rubbish Lister | |
PerlMonks |
Re^4: Use of wantarray Considered Harmfulby graff (Chancellor) |
on Dec 14, 2008 at 04:00 UTC ( [id://730242]=note: print w/replies, xml ) | Need Help?? |
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.
In Section
Meditations
|
|