How to concatenate variables in Perl

后端 未结 6 1133
面向向阳花
面向向阳花 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: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 `$`
    

提交回复
热议问题