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
|
|