How to combine variables in Perl?

Is there any other way to combine variables in perl? I accidentally wrote the following line of code:

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

And this led to the conclusion, for example:

 22 is: 22 

I expected:

 $linenumber is: 22 

So, I thought. It should interpret $linenumber in double quotes as a reference to a variable. (how amazing!)

I'm just wondering:. What are the caveats to using this method and can anyone explain how this works?

+6
source share
6 answers

Variable interpolation occurs when you use double quotes. Therefore, special characters must be escaped. In this case, you need to exit $ :

 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 `$` 
+13
source

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 ..." 
+4
source

In Perl, any string constructed with double quotes will be interpolated, so any variable will be replaced by its value. Like many other languages, if you need to print $ , you will need to avoid it.

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

OR

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

OR

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

Scalar interpolation

+3
source

If you changed the code from

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

to

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

or

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

he will print

 $linenumber is: 22 

What I find useful when I want to print the variable name is to use single quotes so that the variables inside are not translated into their value, which makes it easier to read the code.

+2
source

In formulating this answer, I found this web page that 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 it would be useful for the community, so I ask about it and answer my own question. Other helpful answers are more than welcome!

+1
source

you can backslash $ to print it literally.

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

which prints what you expected. You can also use single quotes if you do not want perl to interpolate variable names, but then "\n" will literally interpolate.

+1
source

Source: https://habr.com/ru/post/921970/


All Articles