如何通过引用将方法返回的散列元素插入到字符串中? [英] How to interpolate hash element via reference returned by a method into a string?
问题描述
我想插入对字符串的散列引用,但此方法不起作用。
如何插入 $ self> Test-> {text}
?
#$ self> Test-> {text}包含test 123 ok
printValue is:$ self-> Test-> {text} \ N; #但不工作
输出:
Test = HASH(0x2948498) - > Test-> {text}
方法调用不会在双引号内部进行插值,所以您最终得到字符串化引用,然后是 - > Test-> {text }
。
简单的做法是利用 print
获取参数列表的事实:
printValue is:,$ self-> Test-> {text},\\\
;
您也可以使用连接:
printValue is:。 $ self> Test-> {text}。 \\\
;
您也可以使用 或者您可以使用这个愚蠢的技巧: I want to interpolate hash reference to string, but this method is not working.
How does one interpolate output:
Method calls won't get interpolated inside double quotes, so you end up with the stringified reference followed by The simple way to do it is to take advantage of the fact that You could also use concatenation: You could also use the tried-and-true Or you can use this silly trick:
这篇关于如何通过引用将方法返回的散列元素插入到字符串中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! printf $ c
$ p $ printfValue is%s \\\
,$ self> Test-> {text} ;
printValue is:@ {[$ self-> Test-> {text}]} \\\
;
$self->Test->{text}
?# $self->Test->{text} contains "test 123 ok"
print "Value is: $self->Test->{text} \n"; # but not working
Test=HASH(0x2948498)->Test->{text}
->Test->{text}
. print
takes a list of arguments:print "Value is: ", $self->Test->{text}, "\n";
print "Value is: " . $self->Test->{text} . "\n";
printf
printf "Value is %s\n", $self->Test->{text};
print "Value is: @{ [ $self->Test->{text} ] }\n";