Previous Page
Next Page

4.4. Escaped Characters

Use named character escapes instead of numeric escapes.

Some ASCII characters that might appear in a stringsuch as DEL or ACK or CANdon't have a "native" Perl representation. When one or more of those characters is required, the standard solution is to use a numeric escape: a backslash followed by the character's ASCII value inside double-quotes. For example, using octal escapes:

    $escape_seq = "\127\006\030Z";       # DEL-ACK-CAN-Z

or hexadecimal escapes:

    $escape_seq = "\x7F\x06\x22Z";       # DEL-ACK-CAN-Z

But not everyone who subsequently reads your code will be familiar with the ASCII values for these characters, which means they will have to rely on the associated comments. That's a real shame too, because both of the previous examples are wrong! The correct sequence was:

    $escape_seq = "\177\006\030Z";       # Octal DEL-ACK-CAN-Z


    $escape_seq = "\x7F\x06\x18Z";       # Hexadecimal DEL-ACK-CAN-Z

Errors like that are particularly hard to track down. Even if you do know the ASCII table by heart, it's still easy to mistakenly type "\127" for DEL because the ASCII code for DEL is 127. At least, in base 10 it is. Unfortunately, backslashed escapes in strings are specified in base 8. And once your brain has accepted the 127-is-DEL relationship, it becomes exceptionally hard to see the mistake. After all, it looks right.

That's why it's better to use named escapes for those characters that have no explicit Perl representation. Named escapes are available in Perl 5.6 and later, and are enabled via the use charnames pragma. Once they're operational, instead of using a numeric escape you can put the name of the required character inside a \N{...} sequence within any double-quoted string. For example:

    use charnames qw( :full );

    $escape_seq = "\N{DELETE}\N{ACKNOWLEDGE}\N{CANCEL}Z";

Note that there's no need for a comment here; when you use the actual names of the characters within the string, the escapes become self-documenting.

    Previous Page
    Next Page