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:

$x{time} = gmtime; #vs %x = ( time => gmtime );
It's no different. And you aren't about to get P5P to change that behaviour.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.