• Aucun résultat trouvé

String Literals

Dans le document Perl: The Complete Reference Second Edition (Page 118-122)

Strings are generally surrounded by either single or double quotes. The effects of the quotes are different, however, and they follow the same rules as the Unix shell. When using single quotes, the value of the string is exactly as it appears—no form of

interpretation or evaluation is performed on the contents of the string (except for

\’and\\).

When double quotes are used, the effects are quite different. For a start, double-quoted strings are subject to backslash and variable interpolation, just as they are under Unix. For example, we can use double-quoted strings and the\n backslash sequence to add newline characters to a string. Other backslash (or escape) sequences supported by Perl are listed in Table 4-1.

FUNDAMENTALS

Code Meaning

\n Newline

\r Carriage return

\t Horizontal tab

\f Form feed

\b Backspace

\a Alert (bell)

\e ESC (escape) character

\XXX Character specified in octal, whereXXXis the character’s ASCII code.

\xXX Character specified in hexadecimal, whereXXis the character’s ASCII code.

\x{XXXX} Character specified in hexadecimal, whereXXXXis the character’s Unicode number.

\cX Control character, whereXis the character—\cCis Control-C.

\N{NAME} Named Unicode character.

Table 4-1. Backslash (Escaped) Character Sequences

78

P e r l : T h e C o m p l e t e R e f e r e n c e

The backslash sequence is often called an escape sequence because you “escape” the normal interpretation.

For example:

$string = 'hello world'; # hello world

$string = 'hello world\n'; # hello world\n

$string = "hello world\n"; # hello world with trailing newline

$string = "\tHello World\a\a\n"; # hello world with preceding tab and

# double bell, with trailing newline

Incidentally, the quotes work across line boundaries, so we could rewrite the second example as

$string = 'hello world ';

The newline from the source is included in the final string. Because of this, you need to be careful when using quotes and ensure that you terminate them properly. One of the most common errors is to embed a quote in your string. For example:

$message = 'Can't write to device';

This will fail because the second quote on the line will terminate the first. You can get around this in both single and double quotes by escaping the quote:

$message = 'Can\'t write to device';

$text = "She said \"I can't do that!\"";

Note that in the last example the single quote is not escaped; this is because a single quote has no meaning within double quotes other than as a quote character; thus we could rewrite the first example:

$message = "Can't write to device";

The same is true in reverse; we could have written the second example:

$text = 'She said "I can\'t do that!"';

You can also modify the case of a string specified within double quotes using a number of translation escapes, as listed in Table 4-2.

The examples that follow demonstrate the use of translation escapes in your strings:

$string = "\uhello world"; # Hello world

$string = "\Uhello world\n"; # HELLO WORLD

$string = "\Uhello\E \LWorld\E\n"; # HELLO world with trailing newline

$string = "\Q'Hello World'\E"; # \'Hello\ World\'

Double-quoted strings are also subject to variable interpolation—this means that you can embed a variable directly into a double-quoted string and have the value of that variable inserted into the string. For example:

$name = 'Martin';

$greeting = "Hello $name!\n"; # Generates Hello Martin!

Note that this technique only works on double-quoted strings:

$greeting = 'Hello $name!\n'; # Generates Hello $name!\n

Note that you can only interpolate scalars and arrays—trying to interpolate an entire hash will only result in the string ‘%hash’ being included in the literal value. See the later section “Interpolation of Array Values” for information on how arrays are interpolated into strings.

FUNDAMENTALS

Code Meaning

\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 (escape) all following non-alphanumeric characters.

\E End a\U,\L, or\Qescape.

Table 4-2. Translation Escapes

Also, because we sometimes want to include a variable name directly within the contents of a string that might otherwise upset Perl’s evaluation of that variable name, we can use one of the rules we saw earlier:

$message = "This is ${name}s computer";

Any identifier within the braces is interpreted as a string, and because it has a leading$ sign, the${name}becomes the full name for the variable we are accessing.

Quotes

The quotes we have already seen in Perl are actually operators—they operate on the embedded string. In the case of single quotes, this leads to the introduction of a string, and in the case of double quotes, it leads to the introduction of a string that has been evaluated according to the escapes and interpolation rules we have already seen.

Perl actually supports a number of quoting operators, most of which do more than simply introduce strings. Not all of the quoting operators interpolate, and each has a customary form. They do, however, share the same basic construction, allowing you to select the quote character that you want to use. The full list is given in Table 4-3.

80

P e r l : T h e C o m p l e t e R e f e r e n c e

FUNDAMENTALS We have already seen examples of the first two mechanisms in Table 4-3 in their

customary forms. One of the problems with the customary forms is that if you want to embed quotes of the same type (i.e. single in single), you must escape the quote. Using the quote operator, you can circumvent this:

$message = q/Can't send to device/;

$text = qq/She said "I can't do this!"/;

The character immediately following each operator is the delimiter—the delimiter specifies the limits of the construct you are creating. You can use any alphanumeric or non-whitespace character (that is, other than space, tab, newline) for a delimiter, but the delimiters must match or complement each other. That means that we could use an exclamation mark

$message = q!Can't send to device!;

or we can use a complementary pair of characters such as parentheses, braces, square brackets, or even angle brackets:

$text = qq{She said "I can't do this!"};

$text = qq<She said "I can't do this!">;

Which one you choose will depend entirely on what you are embedding within the construct, and you’ll probably find that in most instances you end up using the same delimiting character.

Also note that the interpolation rules apply according to Table 4-3:

$message = q!Hello $name\n!; # Still outputs Hello $name\n

$message = qq/Hello $name\n/; # Outputs Hello Martin with a newline

Dans le document Perl: The Complete Reference Second Edition (Page 118-122)