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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|