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
Table 2.1:
Perl Escape Sequences
Escape Sequences |
Description or Character |
\ b |
Backspace |
\ e |
Escape |
\ f |
Form. Feed |
\ n |
Newline |
\ r |
Carriage Return |
\ t |
Tab |
\ v |
Vertical Tab |
\ $ |
Dollar Sign |
\ @ |
Ampersand |
\ 0nnn |
Any Octal byte |
\ xnn |
Any Hexadecimal byte |
\ cn |
Any Control character |
\ l |
Change the next character to lowercase |
\ u |
Change the next character to uppercase |
\ L |
Change the following characters to |
|
lowercase until a \ E |
|
sequence is encountered. |
|
Note that you need to use an |
|
uppercase E here, lowercase |
|
will not work. |
\ Q |
Quote meta-characters as literals. |
\ U |
Change the following characters |
|
to uppercase until a \ E |
|
sequence is encountered. Note that you |
|
need to use an uppercase E |
|
here, |
|
lowercase will not work. |
\ E |
Terminate the \ L, \ Q, |
|
or \ U sequence. |
|
Note that you need to use an |
|
uppercase E here, lowercase will not
work. |
\ \ |
Backslash |
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.
The
\
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!.
The
\
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 \
t and \
n escape
sequences.
This program uses two methods to cause a line break.
- The first is simply to include the line break in the source
code.
- The second is to use the
\
n or newline character.