加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

string – 如何在Perl中连接变量?

发布时间:2020-12-15 21:58:13 所属栏目:大数据 来源:网络整理
导读:是否有不同的方法来连接perl中的变量?我不小心写了以下代码行: print "$linenumber is: n" . $linenumber; 这导致输出如下: 22 is:22 我在期待: $linenumber is:22 所以我想知道.它必须将双引号中的$linenumber解释为对变量的引用. (挺酷的!) 我只是想
是否有不同的方法来连接perl中的变量?我不小心写了以下代码行:
print "$linenumber is: n" . $linenumber;

这导致输出如下:

22 is:
22

我在期待:

$linenumber is:
22

所以我想知道.它必须将双引号中的$linenumber解释为对变量的引用. (挺酷的!)

我只是想知道:使用这种方法有什么警告,有人可以解释一下这是如何工作的吗?

解决方法

使用双引号时会发生变量插值.因此,需要转义特殊字符.在这种情况下,您需要转义$:
print "$linenumber is: n" . $linenumber;

它可以改写为:

print "$linenumber is: n$linenumber";

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读