Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

scalar value of list assignment

by tantalor (Initiate)
on Jan 17, 2012 at 19:05 UTC ( [id://948384]=perlquestion: print w/replies, xml ) Need Help??

tantalor has asked for the wisdom of the Perl Monks concerning the following question:

I'm curious about the behavior of the scalar value of a list assignment expression,

sub foo { my ($a) = qw(first second third); }; printf "in scalar context: %s\n", scalar foo(); printf "in list context: %s\n", join ',', foo();

This gives me,

in scalar context: 3 in list context: first

So it seems like the value of a list assignment in scalar context is the scalar-context value of the list on the right-hand side of the assignment, but the value of the list assignment in list context is the list-context value of list on the left-hand side of the assignment.

Is that correct? Is there a simpler way of putting that?

This is sort of a surprising result. I expected the list and scalar context values to "match", or come from the same place, but in this case it appears they come from different parts of the expression (left-hand/right-hand sides).

Replies are listed 'Best First'.
Re: scalar value of list assignment
by chromatic (Archbishop) on Jan 17, 2012 at 20:11 UTC

    Assignments are expressions, and expressions have return values:

    my $count = () = create_and_return_a_list(); my $x = my $y = calculate_some_value();

    Improve your skills with Modern Perl: the free book.

Re: scalar value of list assignment
by JavaFan (Canon) on Jan 17, 2012 at 20:14 UTC
    The value of a list assignment in scalar context is the number of elements on the right hand side of the assignment.

    This is documented in perlop:

    Similarly, a list assignment in list context produces the list of lvalues assigned to, and a list assignment in scalar context returns the number of elements produced by the expression on the right hand side of the assignment.
    This is not the same as "scalar-context value of the list on the right hand side of the assignment". First of all, there is no list in scalar context (duh! if the context is scalar, it ain't list!), and second, if you mean, "the value of the expression on the RHS of the assignment, but then in scalar context", it's wrong as well, as the value of qw(first second third) in scalar context is third, not 3.
      You're absolutely right, I confused list with array. I was thinking of something more like this,
      sub foo { my @a = qw(first second third); my ($a) = @a; };
      In this case, "the number of elements produced by the expression on the right hand side of the assignment" is the the scalar-context value of the array, or 3.
        In that case, $a will be "first". Now, if you'd remove the parens in the second assignment, the value of $a will be 3.
Re: scalar value of list assignment
by ww (Archbishop) on Jan 17, 2012 at 20:21 UTC
    Perhaps I'm missing something, but how does this differ from what you would expect based on the behavior documented in perldata (at "Context," the second major heading) or, more or less comprehensively, in virtually any introductory-level text (say, "Learning Perl", for example)?

    See also ?node_id=3989;BIT=list%20scalar%20context.

    Update: Duh; forgot to paste...

    #!/usr/bin/perl use strict; use warnings; use 5.012; my @foo = qw(one two three); say @foo; my $val = @foo; say $val; =head output: onetwothree 3 =cut
Re: scalar value of list assignment
by tobyink (Canon) on Jan 17, 2012 at 19:58 UTC

    Yes, I'm quite surprised too. The behaviour seems consistent between 5.6.x and 5.14.x, so I'd assume that it comes as the result of deliberate design decisions.

Re: scalar value of list assignment
by ikegami (Patriarch) on Jan 18, 2012 at 07:13 UTC

    Mini-Tutorial: Scalar vs List Assignment Operator covers this topic in detail.

    So it seems like the value of a list assignment in scalar context is the scalar-context value of the list on the right-hand side of the assignment

    No.

    >perl -E"say scalar( (4,5,6) )" 6 >perl -E"say scalar( ($x,$y)=(4,5,6) )" 3

    List assignment in scalar context evaluates to the number of values to which its RHS evaluates.

    the value of the list assignment in list context is the list-context value of list on the left-hand side of the assignment.

    Yes.

    List assignment in list context evaluates to the values to which its LHS evaluates (as lvalues).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://948384]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found