你做的工作比你必须做的多。无需循环变量。
$_ = '' for ($a, $b, $c);
$_ = 0 for ($d, $e, $f);
($a,$b,$c) = ('') x 3; # ... = ('','','')
($d,$e,$f) = (0) x 3; # ... = (0,0,0)
当然,如果您想将它们设置为undef
($a,$b,$c) = (); # set them to undef
循环变量的唯一原因是,如果您是在代表其他范围的子例程中执行此操作。
sub fill{
my $fill = shift;
$_ = $fill for @_;
return;
}
{
fill( 0, my($d,$e,$f) ); # my($d,$e,$f) = (0) x 3;
}
类似地,而不是通过哈希引用列表:
%$_ = () for \(%a, %b, %c);
# or
%$_ = () for (\%a, \%b, \%c);
只需将它们设置为空列表即可。
(%a,%b,%c) = ();
如果您正确设置变量的范围,您应该很少需要这样做。
my(%a,%b,%c); # <== wrong
sub exmpl{
(%a,%b,%c) = (); # <==
# do something with them
...
}
sub exmpl{
my (%a,%b,%c); # <== correct
# do something with them
...
}