如何通过方法返回的哈希元素将哈希元素插入到字符串中?

dns*_*dns 3 perl hash reference

我想插入对字符串的哈希引用,但这种方法不起作用.如何进行插值$self->Test->{text}

# $self->Test->{text} contains "test 123 ok"
print "Value is: $self->Test->{text} \n";   # but not working
Run Code Online (Sandbox Code Playgroud)

输出:

Test=HASH(0x2948498)->Test->{text} 
Run Code Online (Sandbox Code Playgroud)

fri*_*edo 8

方法调用不会在双引号内插入,因此您最终会得到字符串化的引用->Test->{text}.

这样做的简单方法是利用print一个带有参数列表的事实:

print "Value is: ", $self->Test->{text}, "\n";
Run Code Online (Sandbox Code Playgroud)

您还可以使用串联:

print "Value is: " . $self->Test->{text} . "\n";
Run Code Online (Sandbox Code Playgroud)

你也可以使用久经考验的 printf

printf "Value is %s\n", $self->Test->{text};
Run Code Online (Sandbox Code Playgroud)

或者你可以使用这个愚蠢的技巧:

print "Value is: @{ [ $self->Test->{text} ] }\n";
Run Code Online (Sandbox Code Playgroud)