How to interpolate hash element via reference retu

2019-02-25 23:31发布

I want to interpolate hash reference to string, but this method is not working. How does one interpolate $self->Test->{text} ?

# $self->Test->{text} contains "test 123 ok"
print "Value is: $self->Test->{text} \n";   # but not working

output:

Test=HASH(0x2948498)->Test->{text} 

1条回答
等我变得足够好
2楼-- · 2019-02-25 23:56

Method calls won't get interpolated inside double quotes, so you end up with the stringified reference followed by ->Test->{text}.

The simple way to do it is to take advantage of the fact that print takes a list of arguments:

print "Value is: ", $self->Test->{text}, "\n";

You could also use concatenation:

print "Value is: " . $self->Test->{text} . "\n";

You could also use the tried-and-true printf

printf "Value is %s\n", $self->Test->{text};

Or you can use this silly trick:

print "Value is: @{ [ $self->Test->{text} ] }\n";
查看更多
登录 后发表回答