Thursday, May 1, 2008

Perl: Difference between double and single quote

The following code prints apples and pears using concatenation:
$a = 'apples';
$b = 'pears';
print $a.' and '.$b;

It would be nicer to include only one string in the final print statement, but the line
print '$a and $b';

prints literally $a and $b which isn't very helpful. Instead we can use the double quotes in place of the single quotes:
print "$a and $b";

The double quotes force interpolation of any codes, including interpreting variables. This is a much nicer than our original statement. Other codes that are interpolated include special characters such as newline and tab. The code \n is a newline and \t is a tab.

No comments:

Post a Comment