以下是如何工作的?在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