How come the following works?
print "Property is :" . $property->name("NODE_HOST") . "\n";
but not this:
print "Property is : $property->$name("NODE_HOST")\n";
The compiler complains about the second snippet:
Bareword found where operator expected at ./testProperties.pl line 11, near ""Property is : $property->$name("NODE_HOST"
(Missing operator before NODE_HOST?)
Perl is normally pretty easy-going about taking shortcuts when printing out combinations of strings and variables. $property->name is a call to a class Property which returns the value of the name passed in:
sub name {
my ( $self, $propertyName ) = @_;
my $hash_ref = $self->{_hashref};
my %properties = %$hash_ref;
my $property = $properties{$propertyName};
return $property;
}
sub namecan be simplified. Get rid of%properties and $propertyand just access hashref element directly:return $hash_ref->{$propertyName};. Or even get rid of$hash_refand doreturn $self->{_hashref}->{$propertyName};