in reply to If you believe in Lists in Scalar Context, Clap your Hands
First, some corrections:
$r = 'a'..'z' ; # Case 4 -- error
Maybe, but $r = 5..7; wouldn't be.
$r = (@r, @r) ; $v = (%r, %r) ;the @r and %r are evaluated in Scalar Context
No, they're not.
use strict; use warnings; sub print_context { my $c = ( wantarray() ? 'List' : ( defined(wantarray()) ? 'Scalar' : 'Void' ) ); print("$c\n"); } my $r1 = ( print_context(), print_context() ); # Void # Scalar
However, they would be evaluated in scalar context in the following similar code:
my $r2 = sub { ( print_context(), print_context() ) }->(); # Scalar # Scalar
Now, some questions for you:
in List Context there are a number of things which make lists, including:
- the empty list
- singleton values (including undef)
- literal lists -- two or more values separated by ',', grouped together by '()' if required
- the '..' range operator
- slicing other lists, arrays or hashes
How come array slices and hash slices are in that list, but not arrays and hashes?
Why aren't any named operators (such as grep and map) in that list?
what's wrong with the notion of a List in Scalar Context ?
Given that an array returns a list in list context, how do you place that list in scalar context?
Finally, an answer to your question:
BTW: I'm gagging to know how to describe why this:
$r = () = 1..27 ;
A list assignment ("() = 1..27") in scalar context ("$r =") evaluates to the number of elements to assign.
Finally, and for extra points (and points mean prizes), how does one describe the difference between a list and an array ?
An array is variable.
A list is a piece of code that generates a list operator.
A list is the operator generated from a list in the code.
A list is the result of an operator or function in list context.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: If you believe in Lists in Scalar Context, Clap your Hands
by gone2015 (Deacon) on Oct 24, 2008 at 01:43 UTC | |
by ikegami (Patriarch) on Oct 24, 2008 at 02:23 UTC | |
by gone2015 (Deacon) on Oct 24, 2008 at 11:47 UTC | |
by ikegami (Patriarch) on Oct 24, 2008 at 18:54 UTC | |
by JavaFan (Canon) on Oct 24, 2008 at 12:26 UTC | |
by gone2015 (Deacon) on Oct 24, 2008 at 15:49 UTC | |
| |
by ysth (Canon) on Oct 28, 2008 at 05:55 UTC |