Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[variables] try to clarify the difference between item and list assig…
…nment
  • Loading branch information
moritz committed Jul 9, 2012
1 parent 30231bc commit fa25cbb
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions lib/variables.pod
Expand Up @@ -14,12 +14,12 @@ the documentation in L<List>.
=begin table
Sigil Type constraint Default type Flattens
===== =============== ============ ========
$ Mu (no type constraint) Any No
& Callable Callable No
@ Positional Array Yes
% Associative Hash Yes
Sigil Type constraint Default type Flattens Assignment
===== =============== ============ ======== ==========
$ Mu (no type constraint) Any No item
& Callable Callable No item
@ Positional Array Yes list
% Associative Hash Yes list
=end table
Expand All @@ -29,14 +29,23 @@ Examples:
my @array = 1, 2, 3; # Array-variable with three elements
my %hash = London => 'UK', Berlin => 'Germany';
Assignment to C<%> and C<@>-sigiled variables is special-cased
syntactically. Instead of replacing the variable on the left-hand side with
those of the right-hand side, it empties the variable, and fills it
again with the values from the right-hand side.
There are two types of assignment, I<item assignment> and I<list assignment>.
Both use the equal sign C<=> as operator. The distinction wheter a C<=>
means item or list assignment is based on the syntax of the left-hand side.
(TODO: explain in detail, or do that in L<operators>).
my $item = (1, 2); say $item.WHAT; # Parcel()
my @arr = (1, 2); say @arr.WHAT; # Array()
my %hash = (1, 2); asy %hash.WHAT; # Hash()
Item assignment places the value from the right-hand side into the variable
(container) on the left.
List assignment leaves the choice of what to do to the variable on the left.
For example L<Array> variables (C<@> sigil) empty themselves on list assignment, and then
put all the values from the right-hand side into themselves. Contrary to item
assignment, it means that the type of the variable on the left always stays
C<Array>, regardless of the type of the left-hand side.
Note that the item assignment has tighter precedence than list assigment, and
also tighter than the comma. See L<operators> for more details.
=head1 Twigils
Expand Down

0 comments on commit fa25cbb

Please sign in to comment.