如何在Perl中连接变量 [英] How to concatenate variables in Perl

查看:343
本文介绍了如何在Perl中连接变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Perl中是否有其他方法来连接变量?

Is there a different way to concatenate variables in Perl?

我不小心写了以下代码行:

I accidentally wrote the following line of code:

print "$linenumber is: \n" . $linenumber;

结果为:

22 is:
22

我期望:

$linenumber is:
22

所以我想知道。它必须在双引号中解释 $ linenumber 作为对该变量的引用(多么酷!)。

So then I wondered. It must be interpreting the $linenumber in the double quotes as a reference to the variable (how cool!).

使用此方法的注意事项是什么?如何工作?

What are the caveats to using this method and how does this work?

推荐答案

使用双引号时会发生变量插值。因此,特殊字符需要转义。在这种情况下,您需要转义 $

Variable interpolation occurs when you use double quotes. So, special characters need to be escaped. In this case, you need to escape the $:

print "\$linenumber is: \n" . $linenumber;

可以重写为:

print "\$linenumber is: \n$linenumber";

为避免字符串插值,请使用单引号:

To avoid string interpolation, use single quotes:

print '$linenumber is: ' . "\n$linenumber";  # No need to escape `$`

这篇关于如何在Perl中连接变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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