如何通过引用将方法返回的散列元素插入到字符串中? [英] How to interpolate hash element via reference returned by a method into a string?

查看:82
本文介绍了如何通过引用将方法返回的散列元素插入到字符串中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想插入对字符串的散列引用,但此方法不起作用。
如何插入 $ 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}。 \\\
;

您也可以使用 printf
$ p $ printfValue is%s \\\
,$ 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 $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} 

解决方案

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";

这篇关于如何通过引用将方法返回的散列元素插入到字符串中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆