Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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

by tye (Sage)
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

Replies are listed 'Best First'.
Re^6: Confused as to why the "casting context" is mis-behaving (trap)
by thargas (Deacon) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://866559]
and one hand claps...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2017-05-25 10:52 GMT
Find Nodes?
    Voting Booth?