String Interpolation

Strings are sequences of characters. String literals are usually delimited by either single (') or double (") quotes. Double-quoted string literals are subject to backslash and variable interpolation, and single-quoted strings are not (except for \' and \\, used to put single quotes and backslashes into single-quoted strings). You can embed newlines directly in your strings.

Table 4-1 lists all the backslashed or escape characters that can be used in double-quoted strings.

Table 4-1. Double-quoted string representations

Code

Meaning

\n

Newline

\r

Carriage return

\t

Horizontal tab

\f

Form feed

\b

Backspace

\a

Alert (bell)

\e

ESC character

\033

ESC in octal

\x7f

DEL in hexadecimal

\cC

Ctrl-C

\\

Backslash

\"

Double quote

\u

Force next character to uppercase

\l

Force next character to lowercase

\U

Force all following characters to uppercase

\L

Force all following characters to lowercase

\Q

Backslash all following non-alphanumeric characters

\E

End \U, \L, or \Q

Table 4-2 lists alternative quoting schemes that can be used in Perl. These are useful in diminishing the number of commas and quotes you may have to type, and they allow you not to worry about escaping characters such as backslashes when there are many instances in your data. The generic forms allow you to use any non-alphanumeric, non-whitespace characters as delimiters in place of the slash (/). If the delimiters are single quotes, no variable interpolation is done on the pattern. Parentheses, brackets, ...

Get Perl in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.