On the other hand, perl's basic nature is one of context. You get different things based on different context, just talking about core functions:
It's no different. And you aren't about to get P5P to change that behaviour.$x{time} = gmtime; #vs %x = ( time => gmtime );
Now, I'm not saying "Abandon hope all ye who enter here". It's more like it's your choice: take it as a negative to be purged, or accept it as idiomatic and a quirk of DWIMmery, and understand that context always has mattered, always will, and to keep in mind what context you're in at all times. It's really up to you how you want to take this. Personally, though I find it frustrating whenever I do forget the context, I find it far more liberating the rest of the time when the code just Does What I Mean.
Now, as to whether it's better to have wantarray ? @x : \@x vs wantarray ? @x : $x[0], that really depends. You need to pay attention to the name of the function, as well as its normal usage. Is it normal that the caller will be aware that in certain situations only a single element will be returned, even if the callee isn't going to be able to determine that until it gets there? If so, return $x[0]. As an example, XML parsers. Though the XML parser can never be sure that an element won't be repeated, it's entirely likely that the caller knows that there can only be a single element matching a given xpath or whatever, due to an intimate knowledge of the XML and/or DTD.
Of course, it still leaves on the user a duty to look at the docs before calling the API. But that's not any different than any other API in any other language.
In other words, leave me my flexibility, thanks. :-P
In reply to Re: Use of wantarray Considered Harmful
by Tanktalus
in thread Use of wantarray Considered Harmful
by kyle
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |