2
昨天我寫了一個小程序來解析我的/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
?如果我不寫它,請回到整個行。變量是否會評估其價值?