Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
After reading flyingmoose's reaction to merlyn's post, I wandered through the POD myself to see if any of the behaviors that merlyn discussed are in some way undocumented such that they should come as a surprise. In just about every case, I found what I was looking for. I thought I might as well offer a little more detail on the ones that seemed most interesting.

  • grep: The POD states that in scalar context grep returns the number of times the expression was true. The fact that it is true as many times as the length of the list-context return value is ... well, expected.

  • sort: The POD for sort states that in scalar context the behavior of sort is undefined (which is different from undef. So if merlyn's experience is that it returns undef in scalar context, that behavior can't be relied upon to not change in future Perl implementations.

  • getpwnam: The POD for getpwnam is a little more generic, applying to this entire group of functions. Therefore, its description is a little vague. It states that "in scalar context you get the name, unless the function was a lookup by name, in which case you get the other thing, whatever it is." The example given is $uid = getpwnam($name); ...so I'd say that behavior is documented though a little obscure.

  • select: The POD for select doesn't textually discuss the scalar-context return value of the four arg version of the function. However, there is an example given in the POD, and it is in keeping with merlyn's findings. Update: It is also discussed briefly in the text (thanks ambrus... good catch).

  • caller: The POD for caller states "In scalar context, returns the caller's package name if there is a caller, that is, if we're in a subroutine or eval, or require, and the undefined value otherwise." (undef)

Regarding flyingmoose's dismay over the situation, and his call for improved consistancy, this came up awhile back in What should be returned in scalar context?. I think that the general consensus was that functions should return what it makes sense for them to return in scalar context. In other words, don't get too caught up in trying to make all list-oriented functions return the same thing in scalar context. Not every shoe fits every foot.

To call these behaviors bugs is irrational, since they, in most cases, are doing something useful, and something that's defined in the documentation. The suggestion to not make assumptions is spot-on; that's why the POD is there -- so assumptions don't have to be made.


Dave


In reply to Re: On Scalar Context by davido
in thread On Scalar Context by merlyn

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 cooling their heels in the Monastery: (8)
As of 2024-04-16 09:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found