修改$ _会改变你的初始阵列,因为$ _是一个别名到当前元素。您的代码应如下所示:
my @array = ("donkie", "kong");
my @junk=map {_some_func('blah', $_) } @array;
if (join ('', @junk) !~ /0/)
{ # for example sake this is always true since return 1 from _some_func.
print map { "here: $_\n"; } @array;
}
sub _some_func
{ # for example sake, lets say $f always exists as a file.
my $j = shift;
my $f = shift;
return 0 if !-e $f;
_do_stuff_to_file($f);
return 1;
}
sub _do_stuff_to_file
{
my $f = shift;
local $_;
open(IN, "<",$f);
open(OUT, ">", "$f.new");
while (<IN>)
{
print OUT;
}
close IN;
close OUT;
}
P.S. map返回具有相同元素数量的数组(如果标量是从块返回的)。 grep只返回block为true的元素。
您的问题已经包含了自己的答案 - 您已经解释了它是如何发生的。你是否想改变你的问题到“我该如何避免让东西暴露$ _'?因为那是我们可以回答的问题(而且迈克尔卡尔曼已经有了) – hobbs 2010-08-21 22:02:05
我同意hobbs提出的问题标题@yesterday Could you更改它? – dolmen 2010-08-23 10:03:10