How to concatenate variables in Perl

浪尽此生 提交于 2019-11-27 18:36:26

问题


Is there a different way to concatenate variables in 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 $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?


回答1:


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 `$`



回答2:


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



回答3:


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.

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

OR

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

OR

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

Scalar Interpolation




回答4:


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.




回答5:


When formulating this response, I found this webpage which 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!




回答6:


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.



来源:https://stackoverflow.com/questions/11782435/how-to-concatenate-variables-in-perl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!