1.4. Basic Concepts

A shell script is nothing more than a sequence of shell commands stuffed into a text file. The file is then "made executable" by turning on the execute bit (via chmod +x filename) and then the name of the file is typed at a shell prompt. Bingo, one shell program. For example, a script to run the date command followed by the who command can be created and executed like this:

% echo date >somescript
% echo who >>somescript
% cat somescript
date
who
% chmod +x somescript
% somescript
[output of date followed by who]
%

Similarly, a Perl program is a bunch of Perl statements and definitions thrown into a file. You then turn on the execute bit[2] and type the name of the file at a shell prompt. However, the file has to indicate that this is a Perl program and not a shell program, so you need an additional step.

[2] On UNIX systems, that is. For directions on how to render your scripts executable on non-UNIX systems, see the Perl FAQ or your port's release notes.

Most of the time, this step involves placing the line

#!/usr/bin/perl

as the first line of the file. But if your Perl is stuck in some nonstandard place, or your system doesn't understand the #! line, you'll have a little more work to do. Check with your Perl installer about this. The examples in this book assume that you use this common mechanism.

Perl is mostly a free-format language like C—whitespace between tokens (elements of the program, like print or +) is optional, unless two tokens put together can ...

Get Learning Perl, Second Edition 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.