-->
Previous Table of Contents Next


Strings

Strings are sequences of one or more characters. Usually strings contain alphanumeric characters (such as the numbers from 0 to 9), the upper- and lowercase alphabet, and a handful of punctuation characters. Perl does not distinguish between these characters and nonprintable characters. This means that you can actually use Perl to scan binary files.

Strings are represented in a Perl program in one of two ways: You can enclose a string in double quotation marks or you can enclose a string in single quotation marks. When a string is enclosed in double quotation marks, it behaves in a similar way to strings that are in double quotation marks in C. A backslash can be used in a double quoted string to represent special control characters. A list of the backslash special control characters is shown in Table 28.2.

Table 28.2. Perl’s special control characters.

Character Description

\a Causes an audible beep
\b Inserts a backspace
\cD Allows you to put control characters in a string (in this case Ctrl+D)
\f Inserts a formfeed
\e Inserts an escape character
\E Terminates an \L or \U special control character
\l Causes the next letter to be lowercase
\L Causes all characters following it until the next \E to be lowercase
\n Inserts a newline character
\r Inserts a return character
\t Inserts a tab character
\\ Inserts a backslash character
\” Inserts a double quote character
\u Causes the next letter to be uppercase
\U Causes all characters following it until the next \E to be uppercase

Variable substitution can occur within a string that is enclosed in double quotation marks as well. For example, if you define a variable as


$name=”Doreen”;

that variable can be substituted into a greeting displayed on the screen by typing the following Perl command:


print “Hello $name, how are you?\n”;

This command results in the following output to be displayed on the screen:


Hello Doreen, how are you?

Strings enclosed in single quotation marks differ from strings enclosed in double quotation marks because the single quotation marks hide most special characters from the Perl interpreter. The only special characters that single quotes do not hide completely are the single quote character () and the backslash character (\). When Perl encounters a single quote character within a string that is enclosed in single quotation marks, it assumes that it is the end of the string. This means that if you want to put a single quote in a string that is enclosed in single quotes, you must precede it with a backslash. The same rule applies if you want to put a backslash into a string enclosed in single quotes.

For example, if you want to print the string Don’t do that! to the screen by enclosing it in single quotes, enter the following command:


print ’Don\’t do that!’,”\n”;


Note:  
Remember that if you put a newline character into a single quoted string, it just prints \n and doesn’t insert a newline character.

Just as is the case with numbers, Perl provides several operators that can be used to manipulate and compare strings. Table 28.3 lists Perl’s string operators.

Table 28.3. Perl’s string operators.

Operator Description

op1 . op2 Concatenates op1 and op2
op1 x op2 Repeats op1, op2 times
op1 eq op2 Returns True if op1 equals op2
op1 ge op2 Returns True if op1 is greater than or equal to op2
op1 gt op2 Returns True if op1 is greater than op2
op1 le op2 Returns True if op1 is less than or equal to op2
op1 lt op2 Returns True if op1 is less than op2
op1 ne op2 Returns True if op1 is not equal to op2

File Operators

Just like the shell programming languages, Perl has a number of operators that exist only to test for certain conditions that exist in directories and files. The most useful of these operators are listed in Table 28.4.

Table 28.4. Perl file operators.

Operator Description

-B File is a binary file
-d File is a directory
-e File exists
-f File is an ordinary file
-r File is readable
-s File has a nonzero size
-T File is a text file
-w File is writable
-x File is executable
-z File has zero size

These file operators can be used in any Perl expression. For example, the following code uses an if statement to decide if the .profile file exists in the current directory.


if (-e “.profile”) {

    print “.profile is there\n”;

}


Previous Table of Contents Next