Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

list context

by 7stud (Deacon)
on Dec 06, 2010 at 22:14 UTC ( #875693=perlquestion: print w/ replies, xml ) Need Help??
7stud has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

Here is the code I have a question about:

sub do_stuff { my($x) = ($_[0] || 0); ... }

Do the parentheses around $x do anything? It seems to me that the rhs will always be a scalar, and these are equivalent:

my $x = 10; print $x, "\n"; my ($y) = 10; print $y, "\n"; --output:-- 10 10

Comment on list context
Select or Download Code
Re: list context
by chrestomanci (Priest) on Dec 06, 2010 at 22:26 UTC

    In your example, the round brackets around the $x don't do anything, as the right hand side of the assignment must be in scalar context.

    Perhaps the reason you have that code fragment is that it is similar to one popular way of passing function arguments:

    sub do_stuff { my($x,$y,$z) = @_; ... }

    In one company I worked at, the coding convention extended the idiom to this form (with the comments on each argument.):

    sub do_stuff { my( $x, # X axis coordinate. $y, # Y axis coordinate. $z, # Z axis coordinate. ) = @_; ... }
Re: list context
by chromatic (Archbishop) on Dec 06, 2010 at 22:56 UTC
    Do the parentheses around $x do anything?

    Yes, they make a single-element list.

    It seems to me that the rhs will always be a scalar...

    It will, because the high-precedence or operator (||) imposes scalar context on its operands and evaluates to a scalar.

    Consider, however, the difference between:

    my $x = @some_array; my ($y) = @some_array;

    The lvalue parentheses are significant.

      It will, because the high-precedence or operator (||) imposes scalar context on its operands and evaluates to a scalar.

      Only on the LHS.

      use feature qw( say ); my @s = 0 || scalar('a','b','c'); my @l = 0 || ('a','b','c'); say for @s; say "---"; say for @l;
      c --- a b c

        In this case yes (I was too hasty in my explanation). The operator passes along the context in which it appears to its rhs expression.

Re: list context
by JavaFan (Canon) on Dec 06, 2010 at 23:09 UTC
    Do the parentheses around $x do anything?
    It's one of the few cases where parenthesis actually create a list.
    It seems to me that the rhs will always be a scalar,
    Actually, in this case, it isn't. The context of the RHS of an assignment is determined by the LHS. In fact, assignment in list context is a different operator from assignment in scalar context - although both of them are represented by the same symbol. The important thing is context. The context of an expression is always determined by the context (duh!), and never by the expression itself. If the LHS of an assignment is a list, the RHS is in list context, regardless of how the RHS looks like.
    and these are equivalent
    They are equivalent in the sense they print the same thing. Do you consider:
    my $x = 3 - 1; say $x;
    and
    my $y = length "ab"; say $y;
    to be equivalent?

    In your example, in the first case, $x gets the value of the scalar expression on the RHS of the assignment. In the second case, $y is assigned the value of the first element of the list on the RHS of the assignment. There's a difference. Whether you find the difference significant enough to call it not equivalent, or small enough to call it equivalent, I leave up to you. As long as you know what the difference is, it doesn't matter how you label it.

      It's one of the few cases where parenthesis actually create a list.

      Not directly. The LHS of a list assignment operator is always a list. It doesn't matter whether parens are used or not.

      But indirectly, yes, because my ($x) (as opposed to my $x) is what causes the list assignment operator to be used in the first place.

Re: list context
by LanX (Canon) on Dec 07, 2010 at 00:12 UTC
    in my $x = 10; the = is a scalar assignment which returns the assigned scalar

    in my ($y) = 10; the = is a list assignment which returns the number of assigned elements╣

    That's a crucial trick for iterators, where returned elements might be false (like 0 , '' or undef).

    So

    while ( ($x) = iterator() ) { ... }
    will work as long as anything (i.e. no empty list) is returned, while
    while ( $x = iterator() ) { ... }
    can break if you don't use special magics like "0 but true".

    Cheers Rolf

    UPDATE:

    1) in scalar context!

Re: list context
by ikegami (Pope) on Dec 07, 2010 at 00:13 UTC

    (...)=, my(...)=, @a=, %h= and possibly others cause the list assignment operator to be used instead of the scalar assignment operator. I've already written on the differences between the two.

    In your specific case, there is no functional difference between my ($x) = and my $x = unless the "..." is nothing. If the "..." is nothing, there could be a difference.

    use feature qw( say ); sub wp { my($x) = ($_[0] || 0); } sub np { my $x = ($_[0] || 0); } say wp(3); # 3 say np(3); # 3 say 0+wp(3); # 1 say 0+np(3); # 3

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (15)
As of 2014-07-10 15:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (213 votes), past polls