以下是如何工作的?在Perl中打印函數的返回值
print "Property is :" . $property->name("NODE_HOST") . "\n";
但不是這樣的:
print "Property is : $property->$name("NODE_HOST")\n";
編譯器抱怨的第二個片段:
Bareword found where operator expected at ./testProperties.pl line 11, near ""Property is : $property->$name("NODE_HOST"
(Missing operator before NODE_HOST?)
Perl是正常打印出字符串的組合時很隨和約走捷徑和變量。 $property->name
是一類財產,它返回傳遞的名稱值的電話:
sub name {
my ($self, $propertyName) = @_;
my $hash_ref = $self->{_hashref};
my %properties = %$hash_ref;
my $property = $properties{$propertyName};
return $property;
}
爲什麼downvote? – DVK 2012-02-24 00:02:19
作爲一個側面說明,你的'子名'可以簡化。擺脫'%properties和$ property',直接訪問hashref元素:'return $ hash_ref - > {$ propertyName};'。甚至可以擺脫'$ hash_ref'並執行'return $ self - > {_ hashref} - > {$ propertyName};' – DVK 2012-02-24 00:04:15