Monday, August 3, 2009

Quotes and interpolation

Perl has two kinds of qoutes to delimit the strings, double(" ") and single (' ').

my $fruit = "apple";
my $fruit ='apple' ;

but there is one difference that double quotes interpolate, while single quote doesn't. Interpolation allows to have variables inside double quotes and have those variables replaced with their content.

e.g.
print " my favorite fruit is $fruit \n"; #prints: my favorite fruit is apple

print ' my favorite fruit is $fruit \n'; # prints: my favorite fruit is $fruit

But exception is HASH which don't get interpolated within double quotes.
some special characters also can be interpolated within double quotes like \n, \t, \b ....while in case of single quotes these are merely pair of characters.
escape character(\) can be used to escape some characters within quotes like \\, \', \"
to avoid use of \ you can use qq{} instead of " " and q{} instead of ' '

No comments:

Post a Comment