string 如何在 Perl 中连接变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11782435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:33:38  来源:igfitidea点击:

How to concatenate variables in Perl

stringperlconcatenationstring-concatenation

提问by kentcdodds

Is there a different way to concatenate variables in Perl?

在 Perl 中是否有不同的方式来连接变量?

I accidentally wrote the following line of code:

我不小心写了下面这行代码:

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

And that resulted in output like:

这导致输出如下:

22 is:
22

I was expecting:

我期待:

$linenumber is:
22

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

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

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

使用这种方法有哪些注意事项以及它是如何工作的?

回答by Alan Haggai Alavi

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;

It can be rewritten as:

可以改写为:

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

To avoid string interpolation, use single quotes:

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

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

回答by Zon

I like .=operator method:

我喜欢 .=操作符方法:

#!/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 ..."

回答by Hameed

In Perl any string that is built with double quotes will be interpolated, so any variable will be replaced by its value. Like many other languages if you need to print a $, you will have to escape it.

在 Perl 中,任何用双引号构建的字符串都将被插入,因此任何变量都将被其值替换。像许多其他语言一样,如果您需要打印$,则必须对其进行转义。

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

OR

或者

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

OR

或者

printf "$linenumber is:\n%s", $linenumber;

Scalar Interpolation

标量插值

回答by Joe W

If you change your code from

如果你改变你的代码

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

to

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

or

或者

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

it will print

它会打印

$linenumber is:
22

What I find useful when wanting to print a variable name is to use single quotes so that the variables within will not be translated into their value making the code easier to read.

当我想打印一个变量名时,我觉得有用的是使用单引号,这样里面的变量就不会被转换成它们的值,从而使代码更容易阅读。

回答by kentcdodds

When formulating this response, I found this webpagewhich explains the following information:

在制定此回复时,我发现此网页解释了以下信息:

###################################################
#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.

I thought this would be helpful to the community so I'm asking this and answering my own question. Other helpful answers are more than welcome!

我认为这对社区有帮助,所以我提出这个问题并回答我自己的问题。非常欢迎其他有用的答案!

回答by John Corbett

You can backslash the $to print it literally:

您可以反斜杠$从字面上打印它:

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

That prints what you were expecting. You can also use single quotes if you don't want Perl to interpolate variable names, but then the "\n"will be interpolated literally.

这将打印您所期望的。如果您不想让 Perl 插入变量名,您也可以使用单引号,但随后"\n"将逐字插入。