Perl字符串插值可以执行任何表达式求值吗? [英] Can Perl string interpolation perform any expression evaluation?
本文介绍了Perl字符串插值可以执行任何表达式求值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
与问题有关:如何在Perl中用求值表达式代替? >
在Perl中,有没有一种像Ruby一样的方法:
$a = 1;
print "#{$a + 1}";
它可以打印出2
吗?
解决方案
Perl中有一个类似的速记:
$a = 1;
print "@{[$a + 1]}"
之所以起作用,是因为[]
创建对包含一个元素的数组的引用(计算结果),然后@{}
取消引用该数组,在字符串内插中按顺序打印数组的每个元素.由于只有一个,因此只打印一个元素.
related to question: How do I substitute with an evaluated expression in Perl?
In Perl, is there a way like in Ruby to do:
$a = 1;
print "#{$a + 1}";
and it can print out 2
?
解决方案
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.
这篇关于Perl字符串插值可以执行任何表达式求值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文