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

是否有不同的方法来连接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 `$`

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...