Example: Double-Quoted Strings
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 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
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.
\
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.
0 comments:
Post a Comment