in reply to Re: Not understanding 2 sentences in perldoc in thread Not understanding 2 sentences in perldoc
List assignment in list context produces a list of lvalues:
(my ($x, $y, $z) = qw( 1 2 3 )) = qw( a b c ); # $x = 'a', $y = 'b', $z = 'c'.
What is the lvalues here?
Re^3: Not understanding 2 sentences in perldoc
by LanX (Saint) on Jul 29, 2020 at 20:28 UTC
|
> What is the lvalues here?
($x, $y, $z) are after the first assignment ready for the second one.
Lvalue means left value of assignment (the recipient)
See also perlglossary
- lvalue
Term used by language lawyers for a storage location you can assign a new value to, such as a variable or an element of an array. The “l” is short for “left”, as in the left side of an assignment, a typical place for lvalues. An lvaluable function or expression is one to which a value may be assigned, as in pos($x) = 10 .
| [reply] |
|
"($x, $y, $z) are after the first assignment ready for the next one." The next one is the assignment to qw( a b c ); ?
And what happens to qw( 1 2 3 ) in (my ($x, $y, $z) = qw( 1 2 3 )) = qw( a b c ); # $x = 'a', $y = 'b', $z = 'c'. ?
| [reply] [d/l] [select] |
|
The assignment my ($x, $y, $z) = qw( 1 2 3 ) happens first, i.e. those three variables are assigned the numbers 1 to 3, and this first assignment returns the lvalues, to which the values qw( a b c ) are immediately assigned. Update: In other words, the numbers are assigned to the variables, but then immediately overwritten. Since this is just some example code it doesn't make much sense here, but in theory, you could for example be assigning to tied variables where assigning to the variable has some side effect, then it would make a difference.
| [reply] [d/l] [select] |
|
|
|
|
haukex posted "The assignment my ($x, $y, $z) = qw( 1 2 3 ) happens first, i.e. those three variables are assigned the numbers 1 to 3, and this first assignment returns the lvalues, to which the values qw( a b c ) are immediately assigned."
"and this first assignment returns the lvalues" What lvalues does it return? I see (my ($x, $y, $z) = qw( 1 2 3 )) = qw( a b c ); as # 1 = 'a', 2 = 'b', 3 = 'c'.
| [reply] [d/l] [select] |
|
- ( ... ) = is a list assignment with list context inside the parens
we have two nested list assignments here
- my ($x, $y, $z) = qw( 1 2 3 ) is a list assignment
effect $x=1, $y=2, $z=3
returns lvalues $x, $y, $z in list context
- ( $x, $y, $z ) = qw( a b c ); is a list assignment
effect $x="a", $y="b", $z ="c"
doesn't return because in void context (start statement)
Hints
- If you don't believe it please try it out
- for terminology refer to perlglossary
| [reply] [d/l] [select] |
|
|
|
|
| [reply] |
|
(my ($x, $y, $z) = qw( 1 2 3 )) = qw( a b c );
Is semantically equivalent to this,
my ($x, $y, $z) = qw( 1 2 3 ); # $x = '1', $y = '2', $z = '3'.
($x, $y, $z) = qw( a b c ); # $x = 'a', $y = 'b', $z = 'c'.
| [reply] [d/l] [select] |
Re^3: Not understanding 2 sentences in perldoc
by perlfan (Vicar) on Jul 29, 2020 at 20:48 UTC
|
# | inner assignment with a LHS (l) and RHS (r)
# llllllllll..v...rrrrrrr #
+ $x = 1, $y = 2, $z = 3.
(my ($x, $y, $z) = ( 1, 2, 3 )) = ('a', 'b', 'c' );
# llllllllll.................^..rrrrrrrrrrrrr #
+ $x = 'a', $y = 'b', $z = 'c'.
# | outter assignment with a LHS (l) a
+nd RHS (r)
Slightly changed the internal scalar semantics by eliminating qw.
| [reply] [d/l] [select] |
|
|