Can Perl string interpolation perform any expression evaluation?

岁酱吖の 提交于 2019-11-28 04:40:55

There's a similar shorthand in Perl for this:

$a = 1;
print "@{[$a + 1]}"

This works because the [] creates a reference to an array containing one element (the result of the calculation), and then the @{} dereferences the array, which inside string interpolation prints each element of the array in sequence. Since there is only one, it just prints the one element.

You can use the @{[ EXPRESSION ]} trick that Greg Hewgill mentioned.

There's also the Interpolation module, which allows you to do arbitrary transformations on the values you're interpolating (like encode HTML entities) in addition to evaluating expressions.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!