Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^5: Confused as to why the "casting context" is mis-behaving (trap)

by tye (Cardinal)
on Oct 21, 2010 at 14:16 UTC ( #866559=note: print w/ replies, xml ) Need Help??


in reply to Re^4: Confused as to why the "casting context" is mis-behaving
in thread Confused as to why the "casting context" is mis-behaving

The example from perltrap is unfortunately ambiguous.

Not even the fact that they have effectively the same assignment (from a list to a scalar) and claim that the result will be the length of the list?

No, it doesn't get the length of the list (literal). It gets the length of the array, which is the last expression in the list literal. The last word of the example is worse than ambiguous, it is misleading. One can argue that it isn't technically incorrect because $x does get assigned the length of the list of scalar values currently in the array, @y. But it certainly looks incorrect if you view it through the broken lens of the false dichotomy of "array vs list".

I.E. that note in perltrap is wrong, at least for those versions of perl that I have access to.

If you run the provided code in your copy of Perl 5 the output is not the documented "x = 3"? If so, then you should file a bug report.

The example would not be ambiguous if slightly modified:

Comma operator in scalar context gives scalar context to args

The comma operator in a scalar context is now guaranteed to give scalar context to its arguments.

@y= ('a','b','c','d'); $x = (1, 2, @y); print "x = $x\n";

Perl4 prints: x = c # Thinks list context interpolates list

Perl5 prints: x = 4 # Knows scalar uses length of array

This illustrates why I said "be careful how you define 'item'". A list literal can be said to return its last "item" in scalar context. But it is much more accurate to say that a list literal in a scalar context calls the last expression in scalar context and returns (just) that result. A list literal is a syntax construct so "last item" means the last expression, whatever appears after the last literal comma in that part of the source code.

A list literal can return a list of scalar values. But the last value of that list of values is not always the same as the "last item" that the list literal returns in scalar context. This further proves that a list literal is not the same as a list of scalar values.

A slice in scalar context actually does return the last value. A list literal returns the scalar value of the last expression. The perltrap excerpt illustrates the difference.

- tye        


Comment on Re^5: Confused as to why the "casting context" is mis-behaving (trap)
Download Code
Re^6: Confused as to why the "casting context" is mis-behaving (trap)
by thargas (Chaplain) on Oct 21, 2010 at 14:36 UTC

    Oops. My mistake. I was tricked by the exact ambiguity you note. Thanks for the correction.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2014-04-20 10:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls