Perl Basics

A few fundamentals of Perl that you'll want to keep in mind.

Toby Everett, Bob Wells

December 31, 1997

2 Min Read
ITPro Today logo

Here are a few fundamentals of Perl that you'll want to keep in mind as you examine the script. Perl's key variable types are scalar, array, and hash. A special character in front of the variable name identifies each type. Table A summarizes the main ideas about Perl's variable types.

As you work with Perl, keep in mind that it is case sensitive (i.e., it treats uppercase and lowercase characters as different characters). Thus, Perl recognizes $primary and $Primary as different variables. Perl is also a weakly typed language, which means that you don't need to declare a variable before using it. You can introduce variables anywhere in a Perl script.

Larry Wall, the author of Perl, has studied linguistics extensively. As a result, Perl has many natural language features. As you study Perl, you'll discover that Perl offers alternative ways to say the same thing, and provides many contractions that simplify expressing common actions.

Perlisms in
Perl strings come in many flavors, the most common of which are single-quoted and double-quoted strings. Perl interpolates double-quoted strings, which means that Perl will substitute for escape sequences and other variables in the string. For example, if the third item in the @pets array is "camel", the line

print "Larry's favorite pet is his $pets[2].";

prints the following string on the display: Larry's favorite pet is his camel.

Perl supports short-circuit evaluation of Boolean operators. For example, when asked to evaluate A or B when A is true, Perl skips evaluating B because it doesn't need to evaluate B to verify the value of the expression. Thus, you will commonly see conditional statements such as

open(HANDLE, "which means "open this file or die." Perl offers the more traditional || and && operators from C, as well as or and and. The advantage of or and and is that their precedence level is lower so they're less likely to cause problems if you forget to put parentheses around the two halves of the statement.Perl has excellent support for regular expressions. Regular expressions are a method of expressing string pattern matches. For instance, the line$boo =~ /^[A-Z][a-z]*$/;tests whether $boo contains a capitalized word. The ^ symbol matches the start of the string, [A-Z] matches an uppercase character, [a-z]* matches zero or more lowercase characters, and $ matches the end of the string. For more information about regular expressions, consult the references in the online sidebar, "Perl Resources," at
Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like