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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.