Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re: If you believe in Lists in Scalar Context, Clap your Hands by ikegami
in thread If you believe in Lists in Scalar Context, Clap your Hands by gone2015

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-20 13:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found