How to concatenate variables in Perl?

2019-01-25 14:35发布

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!)

I am just wondering: What are caveats to using this method and could someone explain how this works?

6条回答
forever°为你锁心
2楼-- · 2019-01-25 14:42

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.

查看更多
迷人小祖宗
3楼-- · 2019-01-25 14:43

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.

查看更多
【Aperson】
4楼-- · 2019-01-25 14:58

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 `$`
查看更多
姐就是有狂的资本
5楼-- · 2019-01-25 15:00

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 ..."
查看更多
Summer. ? 凉城
6楼-- · 2019-01-25 15:03

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

查看更多
爷的心禁止访问
7楼-- · 2019-01-25 15:06

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!

查看更多
登录 后发表回答