How to concatenate variables in Perl

后端 未结 6 1112
面向向阳花
面向向阳花 2020-12-18 01:37

Is there a different way to concatenate variables in Perl?

I accidentally wrote the following line of code:

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


        
相关标签:
6条回答
  • 2020-12-18 02:06

    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.

    0 讨论(0)
  • 2020-12-18 02:08

    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

    0 讨论(0)
  • 2020-12-18 02:17

    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 `$`
    
    0 讨论(0)
  • 2020-12-18 02:18

    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 ..."
    
    0 讨论(0)
  • 2020-12-18 02:19

    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!

    0 讨论(0)
  • 2020-12-18 02:21

    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.

    0 讨论(0)
提交回复
热议问题