Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
What I meant by that was that the subroutine should always return the same information - changing context should be lossless. Hence, an array vs. an iterator (or an arrayref). But, not an array vs. the first element.

I still think you are missing the point. The assumption is that in the latter case, that the routine must be calculating the entire array and then discarding most of the data in a scalar context. But that just isn't the case.

In the case of the each sub the routine can detect the scalar context and avoid a potentially costly determination of the value if the user only wants the key. That's a 50% or potentially much higher optimisation. And that for a sub that returns just a 2 element list:

sub each { ... return wantarray ? ( getKey(...), getValue(...) ) : getKey(...); }

For a sub like caller, if the user only wants the calling package name--which probably accounts for 90% of uses--then detecting scalar context to avoid walking a complex AST to gather the rest of the information that the user would only then discard, is again, a very useful optimisation. Think about something like Moose which needs to detect the caller package a lot, and how much more slowly it would run if caller went through the process of gathering all the 11 possible values, everytime the calling code just need the first.


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.

In reply to Re^8: Use of wantarray Considered Harmful by BrowserUk
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2024-03-28 09:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found