A Bit More About Quoting
So
far, whenever we’ve needed to quote a
string we’ve used double quotes (as in,
"this is a double-quoted string"). In fact, Perl
also supports the use of single quotes (as in,
'this is a single-quoted string'). It’s
important for you to understand the difference between the two.
The difference is just this: when it processes a double-quoted
string, Perl looks in it for things that look like variables and
replaces them with the contents of those variables. This process is
called variable
interpolation
. It
also looks for certain sequences beginning with a backslash
(\) and replaces them with special characters. The
sequences are called
backslash
escapes, and the process of replacing them with special
characters is called backslash interpretation.
When it’s processing a single-quoted string Perl doesn’t
bother doing this. You get the string, just like it’s written.
(Actually, Perl processes two backslash escapes within a
single-quoted string: \', which it interprets as a
literal single quote, and \\, which it interprets
as a literal backslash. This lets you put literal single quotes and
literal backslashes inside your string, which would otherwise be
difficult to do.)
Let’s create a new script called quotes.plx
(Example 2-2) to see how this works.
Example 2-2. A script to test how Perl treats single- and double-quoted strings
#!/usr/bin/perl # quotes.plx -- test handling of single- and double-quoted strings $veggies = 'rutabagas'; print "I like to eat ...