有没有办法在perl中的一行代码中打印总和结果?

时间:2012-02-17 13:16:57

标签: string perl

而不是将结果汇总到一行并将其打印在另一行中? 如果我有这个数据

my $a = 1;
my $b = 2;

目前,如果我想打印总和结果,我需要编写像这样的代码

my $tmp = $a+$b;
print "result=$tmp\n";

类似的东西是否与上述相同
print "result=$a+$b\n";

2 个答案:

答案 0 :(得分:4)

是;

print "Result = ", $a+$b, "\n";

答案 1 :(得分:4)

print "Result=", $a+$b, "\n";         # Multi-arg print
print "Result=".($a+$b)."\n";         # Concatenation
print "Result=${\( $a+$b )}\n";       # ref-deref trick (scalar context)
print "Result=@{[ $a+$b ]}\n";        # ref-deref trick (list context)
printf "Result=%s\n", $a+$b;          # printf
print sprintf("Result=%s\n", $a+$b);  # sprintf