Double-quoted strings start out simple, then become a bit more involved than single-quoted strings. With double-quoted strings, you can use the backslash to add some special characters to your string. Chapter 3 will address about how double-quoted strings and variables interact.
The basic double-quoted string is a series of characters surrounded by double quotes. If you need to use the double quote inside the string, you can use the backslash character.
This literal is similar to one you've already seen. Just the quotes are different. Another literal that uses double quotes inside a double-quoted string:
"David said, \"It is fun to learn Perl.\""
Notice how the backslash in the second line is used to escape the double quote characters. And the single quote can be used without a backslash.
One major difference between double- and single-quoted strings is that double-quoted strings have some special escape sequences that can be used. Escape sequences represent characters that are not easily entered using the keyboard or that are difficult to see inside an editor window. The following are all of the escape sequences that Perl understands are given in Table 2.1
|Escape Sequences||Description or Character|
||Any Octal byte|
||Any Hexadecimal byte|
||Any Control character|
||Change the next character to lowercase|
||Change the next character to uppercase|
||Change the following characters to|
|lowercase until a
|sequence is encountered.|
|Note that you need to use an|
|uppercase E here, lowercase|
|will not work.|
||Quote meta-characters as literals.|
||Change the following characters|
|to uppercase until a
|sequence is encountered. Note that you|
|need to use an uppercase E|
|lowercase will not work.|
|Note that you need to use an|
|uppercase E here, lowercase will not work.|
Note In the next chapter we'll see why you might need to use a backslash when using the $ and @ characters.
The examples following the table will illustrate some of them.
"\udave \umarshall is \x35\x years old."
This literal represents the following: Dave Marshall is 35 years old.
\u is used twice in the first word to capitalize the d and m
characters. And the hexadecimal notation is used to represent the age using the
ASCII codes for 3 and 5.
"The kettle was \Uhot\E!"
This literal represents the following: The kettle was HOT!.
\U capitalizes all characters until a
\E sequence is seen.
A final example:
print "Bill of Goods Bread:\t\$34.45\n"; print "Fruit:\t"; print "\$45.00\n"; print "\t======\n"; print "\t\$79.45\n";
Actually, this example isn't too difficult, but it does involve
looking at more than one literal at once and it's been a few pages
since our last advanced example. Let's look at the
This program uses two methods to cause a line break.
\n or newline character.
I recommend using the
\n character so that when looking at your
code in the future, you can be assured that you meant to cause
a line break and did not simply press the ENTER key by mistake.
Caution If you are a C/C++ programmer, this material is not new to you. However, Perl strings are not identical to C/C++ strings because they have no ending NULL character. If you are thinking of converting C/C++ programs to Perl, take care to modify any code that relies on the NULL character to end a string.