1. Strings
Contents:
He multiplieth words without knowledge. - Job 35:16 1.0. IntroductionMany programming languages force you to work at an uncomfortably low level. You think in lines, but your language wants you to deal with pointers. You think in strings, but it wants you to deal with bytes. Such a language can drive you to distraction. Don't despair, though - Perl isn't a low-level language; lines and strings are easy to handle. Perl was designed for text manipulation. In fact, Perl can manipulate text in so many ways that they can't all be described in one chapter. Check out other chapters for recipes on text processing. In particular, see Chapter 6, Pattern Matching , and Chapter 8, File Contents , which discuss interesting techniques not covered here. Perl's fundamental unit for working with data is the scalar, that is, single values stored in single (scalar) variables. Scalar variables hold strings, numbers, and references. Array and hash variables hold lists or associations of scalars, respectively. References are used for referring to other values indirectly, not unlike pointers in low-level languages. Numbers are usually stored in your machine's double-precision floating-point notation. Strings in Perl may be of any length (within the limits of your machine's virtual memory) and contain any data you care to put there - even binary data containing null bytes.
A string is not an array of bytes: You cannot use array subscripting on a string to address one of its characters; use
A scalar value is either defined or undefined. If defined, it may hold a string, number, or reference. The only undefined value is
Two defined strings are
false: the
empty string ("") and a string of length one containing the digit zero ("
The
Specify strings in your program either with single quotes, double quotes, the quote-like operators $string = '\n'; # two characters, \ and an n $string = 'Jon \'Maddog\' Orwant'; # literal single quotes
Double quotes interpolate variables (but not function calls - see
Recipe 1.10
to find how to do this) and expand a lot of backslashed shortcuts: " $string = "\n"; # a "newline" character $string = "Jon \"Maddog\" Orwant"; # literal double quotes
The $string = q/Jon 'Maddog' Orwant/; # literal single quotes You can use the same character as delimiter, as we do with / here, or you can balance the delimiters if you use parentheses or paren-like characters: $string = q[Jon 'Maddog' Orwant]; # literal single quotes $string = q{Jon 'Maddog' Orwant}; # literal single quotes $string = q(Jon 'Maddog' Orwant); # literal single quotes $string = q<Jon 'Maddog' Orwant>; # literal single quotes "Here documents" are borrowed from the shell. They are a way to quote a large chunk of text. The text can be interpreted as single-quoted, double-quoted, or even as commands to be executed, depending on how you quote the terminating identifier. Here we double-quote two lines with a here document: $a = <<"EOF"; This is a multiline here document terminated by EOF on a line by itself EOF
Note there's no semicolon after the terminating A warning for non-Western programmers: Perl doesn't currently directly support multibyte characters (expect Unicode support in 5.006), so we'll be using the terms byte and character interchangeably. Copyright © 2001 O'Reilly & Associates. All rights reserved. |
|