Please help me understand the following snippets:
my $count = @array;
my @copy = @array;
my ($first) = @array;
(my $copy = $str) =~ s/\\/\\\\/g;
my ($x) = f() or die;
my $count = () = f();
print($x = $y);
print(@x = @y);
Please help me understand the following snippets:
my $count = @array;
my @copy = @array;
my ($first) = @array;
(my $copy = $str) =~ s/\\/\\\\/g;
my ($x) = f() or die;
my $count = () = f();
print($x = $y);
print(@x = @y);
[This answer is also found in table format here.]
The symbol =
is compiled into one of two assignment operators:
aassign
) is used if the left-hand side (LHS) of a =
is some kind of aggregate.sassign
) is used otherwise.The following are considered to be aggregates:
(...)
)@array
)@array[...]
)%hash
)@hash{...}
)my
, our
or local
There are two differences between the operators.
The two operators differ in the context in which their operands are evaluated.
The scalar assignment evaluates both of its operands in scalar context.
# @array evaluated in scalar context.
my $count = @array;
The list assignment evaluates both of its operands in list context.
# @array evaluated in list context.
my @copy = @array;
# @array evaluated in list context.
my ($first) = @array;
The two operators differ in what they return.
The scalar assignment ...
... in scalar context evaluates to its LHS as an lvalue.
# The s/// operates on $copy.
(my $copy = $str) =~ s/\\/\\\\/g;
... in list context evaluates to its LHS as an lvalue.
# Prints $x.
print($x = $y);
The list assignment ...
... in scalar context evaluates to the number of scalars returned by its RHS.
# Only dies if f() returns an empty list.
# This does not die if f() returns a
# false scalar like zero or undef.
my ($x) = f() or die;
# $counts gets the number of scalars returns by f().
my $count = () = f();
... in list context evaluates to the scalars returned by its LHS as lvalues.
# Prints @x.
print(@x = @y);
© 2022 - 2024 — McMap. All rights reserved.
my ($first) = @array
– Elegy