Chapter 4. The Perl Language

This chapter is a quick and merciless guide to the Perl language itself. If you’re trying to learn Perl from scratch, and you’d prefer to be taught rather than to have things thrown at you, then you might be better off with Learning Perl by Randal Schwartz and Tom Christiansen, or Learning Perl on Win32 Systems by Randal Schwartz, Erik Olson, and Tom Christiansen. However, if you already know some other programming languages and just want to hear the particulars of Perl, this chapter is for you. Sit tight, and forgive us for being terse: we have a lot of ground to cover.

If you want a more complete discussion of the Perl language and its idiosyncrasies (and we mean complete), see Programming Perl by Larry Wall, Tom Christiansen, and Randal Schwartz.

Program Structure

Perl is a particularly forgiving language, as far as program layout goes. There are no rules about indentation, newlines, etc. Most lines end with semicolons, but not everything has to. Most things don’t have to be declared, except for a couple of things that do. Here are the bare essentials:

Whitespace

Whitespace is required only between items that would otherwise be confused as a single term. All types of whitespace—spaces, tabs, newlines, etc.—are equivalent in this context. A comment counts as whitespace. Different types of whitespace are distinguishable within quoted strings, formats, and certain line-oriented forms of quoting. For example, in a quoted string, a newline, a space, and a tab are interpreted as unique characters.

Semicolons

Every simple statement must end with a semicolon. Compound statements contain brace-delimited blocks of other statements and do not require terminating semicolons after the ending brace. A final simple statement in a block also does not require a semicolon.

Declarations

Only subroutines and report formats need to be explicitly declared. All other user-created objects are automatically created with a null or 0 value unless they are defined by some explicit operation such as assignment. The -w command-line switch will warn you about using undefined values.

You may force yourself to declare your variables by including the use strict pragma in your programs (see Chapter 8, for more information on pragmas and strict in particular). This makes it an error to not explicitly declare your variables.

Comments and documentation

Comments within a program are indicated by a pound sign (#). Everything following a pound sign to the end of the line is interpreted as a comment.

Lines starting with = are interpreted as the start of a section of embedded documentation (pod), and all subsequent lines until the next =cut are ignored by the compiler. See Section 4.11 later in this chapter for more information on pod format.

Get Perl in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.