ike*_*ami 4 perl assignment-operator
请帮助我理解以下片段:
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);该符号=被编译为两个赋值运算符之一:
aassign)用于若的左手侧(LHS)=是某种骨料。sassign)被以其它方式使用。以下被认为是聚合:
(...))@array)@array[...])%hash)@hash{...})my,our或local运营商之间有两个区别。
这两个运算符在其操作数被评估的上下文中有所不同。
标量赋值在标量上下文中计算它的两个操作数。
# @array evaluated in scalar context.
my $count = @array;
Run Code Online (Sandbox Code Playgroud)列表赋值在列表上下文中评估它的两个操作数。
# @array evaluated in list context.
my @copy = @array;
Run Code Online (Sandbox Code Playgroud)
# @array evaluated in list context.
my ($first) = @array;
Run Code Online (Sandbox Code Playgroud)这两个运算符的返回内容不同。
标量赋值...
...在标量上下文中将其 LHS 计算为左值。
# The s/// operates on $copy.
(my $copy = $str) =~ s/\\/\\\\/g;
Run Code Online (Sandbox Code Playgroud)... 在列表上下文中将其 LHS 计算为左值。
# Prints $x.
print($x = $y);
Run Code Online (Sandbox Code Playgroud)列表分配...
... 在标量上下文中计算其 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;
Run Code Online (Sandbox Code Playgroud)
# $counts gets the number of scalars returns by f().
my $count = () = f();
Run Code Online (Sandbox Code Playgroud)... 在列表上下文中计算其 LHS 作为左值返回的标量。
# Prints @x.
print(@x = @y);
Run Code Online (Sandbox Code Playgroud)