【发布时间】:2012-05-20 11:36:46
【问题描述】:
昨天我写了一个小子程序来解析我的 /etc/hosts 文件并从中获取主机名。
这是子程序:
sub getnames {
my ($faculty, $hostfile) = @_;
open my $hosts ,'<', $hostfile;
my @allhosts = <$hosts>;
my $criteria = "mgmt." . $faculty;
my @hosts = map {my ($ip, $name) = split; $name} grep {/$criteria/} @allhosts; # <-this line is the question
return @hosts;
}
我将其称为 getnames('foo','/etc/hosts') 并取回与 mgmt.foo 正则表达式匹配的主机名。
问题是,为什么我必须在map 表达式中单独写$name?如果我不写它,请收回整行。变量是否计算出它的值?
【问题讨论】: