Understanding Perl Strings - dummies

By Paul Hoffman

Text items in Perl are called strings. A string is zero or more characters put together in a single chunk. A character is anything that you can type, such as letters, digits, punctuation, and spaces. Strings appear in variables and just as themselves, in which case they are called string literals. An example of a one-character string literal is the letter d. An example of a five-character string is vbn59. Perl variables can hold strings or numbers.

The length of a Perl string is practically unlimited. That’s right, you can make Perl strings as long as you want. Then again, you don’t have to give them any length at all. A zero-character string is as a null string. “A textless string, that’s odd!” you say? Here’s how that works:

If you have a variable called $Short, for example, and that variable has a value that is one character, you can remove the single character from the variable and get a smaller value that has zero characters in it. The variable doesn’t disappear because Perl knows to how remove characters while retaining the variable. Similarly, if you want to add to a variable character by character, you can start with no characters and build from there.