Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Some Insights from a Traveler Between Languages

by TimToady (Parson)
on Apr 23, 2005 at 19:28 UTC ( #450779=note: print w/replies, xml ) Need Help??


in reply to Some Insights from a Traveler Between Languages

In the first case, you get a tuple assigned to foo, and in the second case a list assigned to bar. There's nothing confusing about this.

Except you've just forced the new user to learn the difference between mutable and immutable. Perl largely succeeds in hiding this difference from new users.

In the third case, you end up with a variable that holds the length of the array on the RHS.

Only by accident, in this case. Perl 5 is returning the final value of the list, on the assumption that the user meant to use a C-style comma operator, which is a bad assumption in this case. Perl 6 does not have the C-style comma operator, so you automatically get a reference to the list into $baz. If you use that reference in numeric context, you'll get the length, but if you use it as if it were a tuple, it'll behave as a tuple, and if you use it as a boolean, it'll still behave reasonably.

@biz will still end up with a single element, since it is still assumed that if you use [...] in list context, you really mean it. But if you want more Pythonly semantics for all of those, just use the new := binding operator instead of =, since Python really only has binding, not assignment.

Perl's built-in "length" function is downright silly.

Indeed, I agree, but for different reasons. Perl 6 will not have a length function at all, because "length" is too general a concept in the age of Unicode. You have to be more specific, and specify the units. So we have the following methods:

$str.bytes # length in bytes $str.codes # length in codepoints $str.graphs # length in graphemes $str.chars # length in characters @array.elems # length in elements
Note that this also lets you ask for things like
@array.chars # length of array in characters
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."

Perl is easily my favorite language. However, it would clearly seem to have dropped the ball in these regards.

I hope you will discover that Perl 6 has again picked up most of the balls it dropped in the past, hopefully without dropping too many new balls. It would be possible (though unpopular) to go back through all the recent nodes on PM that complain about any trap in Perl 5, and add annotations about whether Perl 6 addresses the problem. My rough estimate is that at least 90% of those annotations would say "Fixed in Perl 6". I have to sit on myself not to follow up every time I see one of those nodes. It's hard, because I like to brag. That's the problem with Hubris... :-)

Replies are listed 'Best First'.
Re^2: Some Insights from a Traveler Between Languages
by chromatic (Archbishop) on Apr 23, 2005 at 19:43 UTC
    I have to sit on myself not to follow up every time I see one of those nodes.

    That's also fairly difficult, at least in a Euclidean space.

Re^2: Some Insights from a Traveler Between Languages
by dragonchild (Archbishop) on Apr 23, 2005 at 19:48 UTC
    It's hard, because I like to brag. That's the problem with Hubris... :-)

    Of everyone who has ever posted on this Monastery, I think that you have the most right to brag. In fact, I'd almost say you have an obligation to brag. *grins*

Re^2: Some Insights from a Traveler Between Languages
by doom (Deacon) on Apr 24, 2005 at 20:55 UTC
    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...)

Re^2: Some Insights from a Traveler Between Languages
by skyknight (Hermit) on Apr 23, 2005 at 20:55 UTC
    I am very much looking forward to the release of Perl 6. I have high hopes that it will still be everything I love about Perl but with significantly less nonsense. Here's to unreasonable expectations! :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://450779]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2020-07-08 11:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?