Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
On the question of why $length = length(@array) can't do what you expect it to do:
But having said all that, I also agree with your reason, which is why Perl 6 no longer forces evaluation of scalars in scalar context, but autoenreferences anything it can autoenreference. The underlying problem with Perl 5's length(@array) is that it's forcing evaluation of @array in generic scalar context without knowing whether the array is eventually going to be used in a boolean, string, array, or reference context. Perl 6 straightens out this mess without breaking the behavior that Perl 5 programmers actually want. They don't want an array to return its length in scalar context. They want the array to return its length in numeric context. And they don't actually want its length in boolean context--they merely want to know if the array thinks of itself as "true". In Perl 6, scalar context means "I don't know how this object is going to be used yet, so let's not commit yet."
Isn't easier to just say "Yeah, Larry blew it here, but it's being fixed in perl 6"? (Update: though actually, that essentially is what you were saying... sorry, need to read more carefully.)

Perl's polymorphism based on "context" rather than "type" can genuinely be pretty confusing... maybe this is a case where DWIM should've won out over consistency.

Anyway, it sounds like perl 6 will clear this up in a few different ways: (1) there are more contexts (numeric, string, etc), so it's easier for something to do the Right Thing, (2) functions like "length" are turning into object methods (or properties?), so you can make it clear what you're trying to get the length of: @array.length.

The array length business really is a bit of a mess in perl 5. "How do you get the length of an array" is one of the first questions I seem to get from beginning perl programmer's; and speaking for myself it took me forever to get to the point where I could remember which of the two ways was which. (Of course, one of the reasons they ask that question is that they think they need to iterate explicitly over the array, but that's another story.)

Lots of other languages seem more sensible in this respect... e.g. I was doing some emacs lisp programming recently, where "lists" and "strings" are both types of "sequences", and "length" gets you the property of a sequence, so it works on both, no problem. (Though there I had a problem with looking through the section of the manual on string processing to try and find the "length" function, when really it's off in the section on sequences...)


In reply to Re^2: Some Insights from a Traveler Between Languages by doom
in thread Some Insights from a Traveler Between Languages by skyknight

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 making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-03-29 00:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found