Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^3: If you believe in Lists in Scalar Context, Clap your Hands

by chromatic (Archbishop)
on Oct 24, 2008 at 08:27 UTC ( #719276=note: print w/ replies, xml ) Need Help??


in reply to Re^2: If you believe in Lists in Scalar Context, Clap your Hands
in thread If you believe in Lists in Scalar Context, Clap your Hands

Are there cases where something that's unambiguously a list (and not an array) returns anything else than the last item?

A regular expression match with the /g flag, in scalar context.

A map or grep expression in scalar context.

The empty list my $x = () = some_expression; construct, in scalar context.

split, chomp, ....

A bare hash. (Want to argue over what a list is? Is it multiple, comma-separated expressions or something that pushes one or more items onto the internal stack? I'm not sure lists even exist as a language-level construct in Perl 5.)


Comment on Re^3: If you believe in Lists in Scalar Context, Clap your Hands
Select or Download Code
Re^4: If you believe in Lists in Scalar Context, Clap your Hands
by moritz (Cardinal) on Oct 24, 2008 at 15:28 UTC
    One could argue that built-ins like map and grep don't return a list in scalar context, but are context sensitives themselves, so they are not fit for explaining "a list in scalar context". Likewise a regex match with /g.

      Sure, but you can also argue that 1, 2, 3, isn't a list of values but a list of expressions. Is this a list:

      my $x = ($y = 1), ($z = 2);

      How about this?

      s{\\}{\\\\}g, print

      Or this?

      foo => scalar param( 'foo' )

      It's difficult to tell without seeing the context... which makes me wonder if lists do exist, or if we should talk about the value of expressions instead.

Re^4: If you believe in Lists in Scalar Context, Clap your Hands
by jwkrahn (Monsignor) on Oct 25, 2008 at 02:15 UTC

    chomp???   How did chomp get in there?   It never returns a list.

      when used with a list, it returns the total number of characters chomped... and not a list (so, you are right that there is no "list context" to it.
      []s, HTH, Massa (κς,πμ,πλ)
      my @x = chomp <DATA>;

      Bingo, @x is gets a one-element list -- or at least, it's a list in list context and a scalar in scalar context. Tricky! Here's a mind-bender. What's this?

      1, 2, 3

      It could be a three-element list, in list context, or it could be a scalar and two void expressions (which get optimized away), in scalar context. In void context, it could be nothing.

      Update: Reworded one sentence.

        So, then, scalar also returns a list in list context?   (BTW, @x is an array, not a one element list.)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://719276]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2014-08-22 01:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (145 votes), past polls