String 如何在Perl中连接变量
在Perl中是否有不同的方式连接变量 我无意中编写了以下代码行:String 如何在Perl中连接变量,string,perl,concatenation,string-concatenation,String,Perl,Concatenation,String Concatenation,在Perl中是否有不同的方式连接变量 我无意中编写了以下代码行: print "$linenumber is: \n" . $linenumber; 这就产生了如下输出: 22 is: 22 我期待着: $linenumber is: 22 于是我想知道。它必须将双引号中的$linenumber解释为对变量的引用(太酷了!) 使用此方法的注意事项是什么,以及它是如何工作的?在制定此响应时,我发现这解释了以下信息: ######################################
print "$linenumber is: \n" . $linenumber;
这就产生了如下输出:
22 is:
22
我期待着:
$linenumber is:
22
于是我想知道。它必须将双引号中的$linenumber
解释为对变量的引用(太酷了!)
使用此方法的注意事项是什么,以及它是如何工作的?在制定此响应时,我发现这解释了以下信息:
###################################################
#Note that when you have double quoted strings, you don't always need to concatenate. Observe this sample:
#!/usr/bin/perl
$a='Big ';
$b='Macs';
print 'I like to eat ' . $a . $b;
#This prints out:
# I like to eat Big Macs
###################################################
#If we had used double quotes, we could have accomplished the same thing like this:
#!/usr/bin/perl
$a='Big ';
$b='Macs';
print "I like to eat $a $b";
#Printing this:
# I like to eat Big Macs
#without having to use the concatenating operator (.).
###################################################
#Remember that single quotes do not interpret, so had you tried that method with single quotes, like this:
#!/usr/bin/perl
$a='Big ';
$b='Macs';
print 'I like to eat $a $b';
#Your result would have been:
# I like to eat $a $b
#Which don't taste anywhere near as good.
我认为这会对社区有所帮助,所以我提出这个问题并回答我自己的问题。其他有用的答案非常受欢迎 您可以将
$
反斜杠以逐字打印:
print "\$linenumber is: \n" . $linenumber;
这是你所期待的。如果不希望Perl插入变量名,也可以使用单引号,但是
“\n”
将按字面插入。使用双引号时会发生变量插入。因此,需要对特殊字符进行转义。在这种情况下,您需要转义$
:
print "\$linenumber is: \n" . $linenumber;
它可以重写为:
print "\$linenumber is: \n$linenumber";
要避免字符串插值,请使用单引号:
print '$linenumber is: ' . "\n$linenumber"; # No need to escape `$`
在Perl中,任何使用双引号构建的字符串都将被插值,因此任何变量都将被其值替换。与许多其他语言一样,如果您需要打印
$
,则必须对其进行转义
print "\$linenumber is:\n$linenumber";
或
或
如果您从
print "$linenumber is: \n" . $linenumber;
到
或
它会打印出来
$linenumber is:
22
当我想要打印变量名时,我发现有用的是使用单引号,这样里面的变量就不会被转换成它们的值,从而使代码更容易阅读 我喜欢
=
运算符方法:
#!/usr/bin/perl
use strict;
use warnings;
my $text .= "... contents ..."; # Append contents to the end of variable $text.
$text .= $text; # Append variable $text contents to variable $text contents.
print $text; # Prints "... contents ...... contents ..."
这是一个很好的观点。有件事我忘了提。我想这不完全是我的问题+1.无论如何,使用单引号(如您所建议的)的问题是它会逐字解释换行符,请参见我的编辑。从技术上讲,您不需要连接变量,因为
print
可以接受多个参数<代码>打印“$linenumber为:\n”,$linenumber代码>也可以工作。(尽管您需要注意内置变量$,
(请参见man perlvar
)。'$linenumber是:\n'
不会做您想做的事情:-)当插入变量且变量名称边界不清晰时(例如我的$page=12;打印“$pagep.”
),您需要将变量名括在大括号内${…}
。即打印“${page}p.”
print '$linenumber is:' . "\n" . $linenumber;
print '$linenumber is:' . "\n$linenumber";
$linenumber is:
22
#!/usr/bin/perl
use strict;
use warnings;
my $text .= "... contents ..."; # Append contents to the end of variable $text.
$text .= $text; # Append variable $text contents to variable $text contents.
print $text; # Prints "... contents ...... contents ..."