How to concatenate variables in Perl

后端 未结 6 1111
面向向阳花
面向向阳花 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: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!

提交回复
热议问题