Skip to content

Commit 7fa81a3

Browse files
committed
Merge pull request #95 from Zhtwn/assignment-precedence
Clarify item vs list assignment in Variables
2 parents ec67361 + e46780a commit 7fa81a3

File tree

1 file changed

+52
-6
lines changed

1 file changed

+52
-6
lines changed

lib/Language/variables.pod

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ L<sigilless variables|#Sigilless variables>.
3939
There are two types of assignment, I<item assignment> and I<list
4040
assignment>. Both use the equal sign C<=> as operator. The distinction
4141
whether an C<=> means item or list assignment is based on the syntax of the
42-
left-hand side. (TODO: explain in detail, or do that in L<operators>).
42+
left-hand side.
4343
4444
Item assignment places the value from the right-hand side into the variable
4545
(container) on the left.
@@ -52,13 +52,59 @@ themselves. Contrary to item assignment, it means that the type of the
5252
variable on the left always stays C<Array>, regardless of the type of the
5353
right-hand side.
5454
55-
Note that item assignment has tighter precedence than list assignment and
56-
also tighter than the comma. Thus:
55+
The type of assignment (item or list) is decided by the first context
56+
seen in the current expression or declarator:
5757
58-
my @array = my $num = 42, "str";
58+
my $foo = 5; # item assignment
59+
say $foo.perl; # 5
5960
60-
assigns C<42> to C<$num>, and both C<42> and C<"str"> to C<@array>. See
61-
L<operators> for more details.
61+
my @bar = 7, 9; # list assignment
62+
say @bar.WHAT; # Array
63+
say @bar.perl; # [7, 9]<>
64+
65+
(my $baz) = 11, 13; # list assignment
66+
say $baz.WHAT; # Parcel
67+
say $baz.perl; # (11, 13)
68+
69+
Thus, the behavior of an assignment contained within a list assignment depends
70+
on the expression or declarator that contains it.
71+
72+
For instance, if the internal assignment is a declarator, item assignment
73+
is used, which has tighter precedence than both the comma and the list
74+
assignment:
75+
76+
my @array;
77+
@array = my $num = 42, "str"; # item assignment: uses declarator
78+
say @array.perl; # [42, "str"]<> (an Array)
79+
say $num.perl; # 42 (a Num)
80+
81+
Similarly, if the internal assignment is an expression that is being
82+
used as an initializer for a declarator, the context of the internal
83+
expression determines the type of assignment:
84+
85+
my $num;
86+
my @array = $num = 42, "str"; # item assignment: uses expression
87+
say @array.perl; # [42, "str"]<> (an Array)
88+
say $num.perl; # 42 (a Num)
89+
90+
my ( @foo, $bar );
91+
@foo = ($bar) = 42, "str"; # list assignment: uses parens
92+
say @foo.perl; # [42, "str"]<> (an Array)
93+
say $bar.perl; # $(42, "str") (a Parcel)
94+
95+
However, if the internal assignment is neither a declarator nor an
96+
expression, but is part of a larger expression, the context of the
97+
larger expression determines the type of assignment:
98+
99+
my ( @array, $num );
100+
@array = $num = 42, "str"; # list assignment
101+
say @array.perl; # [42, "str"]<> (an Array)
102+
say $num.perl; # [42, "str"]<> (an Array)
103+
104+
This is because the whole expression is C<@array = $num = 42, "str">, while
105+
C<$num = 42> is not is own separate expression.
106+
107+
See L<operators|Language/operators> for more details on precedence.
62108
63109
=head2 Sigilless variables
64110

0 commit comments

Comments
 (0)