A First Perl Program
So, to begin our study of Perl, let us consider a small Perl
program. Do not worry that you are not familiar with all the syntax
used here. The syntax will be introduced more formally as we continue
this book. Just try to infer the behavior of the constructs below as
best you can.
For our first Perl program, we will ask the user their
print out a message greeting the user by name.
use strict; # important pragma
use warnings; # another important pragma
print "What is your username? "; # print out the question
my ; # "declare" the variable
= <STDIN>; # ask for the username
chomp(); # remove "new line"
print "Hello, .\n"; # print out the greeting
# Now we have said hello to our user
Let us examine this program line by line to ascertain its
hand-waving will be necessary, since some of the concepts will not be
presented until later. However, this code is simple enough that you
not yet understand completely what each line is doing.
The first line is how the program is identified as a Perl
Perl programs should start with a line like
Usually, it is just
should put this line at
the top of each of your Perl programs.
In the lines that follow, halfway through each line, there is
character. Everything from the
until the end of the
line is considered a comment. You are not
required to comment
each line. In fact, commenting each line is rare. However, you will
find in this text that we frequently put comments on every line, since
we are trying to explain to the reader exactly what each Perl statement
is doing. When you write Perl programs, you should provide comments,
but you need not do so as verbosely as we do in this text.
Note, too, that comments can also occur on lines by
last line of the program above is an example of that.
Now, consider the code itself, ignoring everything that
# character. Notice that each line (ignoring
comments) ends with
;. This is the way that you tell Perl that
is complete. We\'ll talk more about statements soon; for now, just
consider a statement to be a single, logical command that you give to
The first line,
use strict, is
called a pragma in Perl. It
is not something that "explicitly" gets executed, from your point of
view as the programmer. Instead, a pragma specifies (or changes) the
rules that Perl uses to understand the code that follows. The
strict; pragma enforces the strictest possible rules for
code. You should always use this pragma while you are still new to
Perl, as it will help you find the errors in your code more easily.
The second line is another pragma,
tells Perl that you\'d like to be warned as much as possible when you
write code that might be questionable. Certain features of Perl can
confuse new (and sometimes even seasoned) Perl programmers. The
use warnings pragma, like
strict, is a way to tell
Perl that you\'d like to be warned at run-time when certain operations
So, you might wonder why two separate pragmas are needed. The
that they are enforced by Perl at different times. The
strict pragma enforces compile-time constraints on the
code. You can even test them without running the program by using
perl -c filename,
where filename is the file
containing your program. That option does not run your program, it
merely checks that they syntax of your program is correct. (To remember
this, remember that the letter
"check the program".)
By contrast, the
use warnings pragma
use warnings, messages could
be printed while
your program runs, if Perl notices something wrong. In addition,
different inputs to the program can cause different messages to be
printed (or suppress such messages entirely).
The third line is the first statement of the program the
action directly. It is a call to Perl\'s built-in
function. In this case, it is taking a string (enclosed in double
quotes) as its argument, and sending that string to the standard
which is, by default, the terminal, window, or console from which the
program is run.
The next line is a variable declaration.
mode (set by the
use strict pragma), all
variables must be
declared. In this case, Perl\'s
my keyword is
used to declare
. A variable like
that starts with a
$ is said to be a scalar
more information on scalar variables, see Working
with Scalars. For now, just be aware that scalar variables
can hold strings.
The next line,
is an assignment statement,
which is denoted by the
=. The left hand side
of the assignment
is that scalar variable,
, that we
declared in the line
before it. Since
is on the left
hand side of the
=, that indicates
will be assigned a new value
by this assignment statement.
The right hand side of the assignment is a construct that
allows us to
get input from the keyboard, the default standard input.
is called a file handle that represents the
standard input. We
will discuss more about file handles later. For now, just remember that
assigned to a scalar variable, places
the next line of standard input into that scalar variable.
Thus, at this point, we have the next line of the input (which
hopefully the username that we asked for), in the
variable. Since we got the contents of
standard input, we know that the user hit return after typing her
username. The return key inserts a special character, called newline,
at the end of the line. The
variable contains the
full contents of the line, which is not just the user\'s name, but also
that newline character.
To take care of this, the next thing we do is
Perl\'s built-in function,
chomp, removes any
that are on the end of a variable. So, after the
operation, the variable
The final statement is another
statement. It uses the
value of the
variable to greet the
user with her
name. Note that it is acceptable to use
the string to be printed, and the contents of that scalar are included.
This ends our discussion of our small Perl program. Now that
some idea of what Perl programs look like, we can begin to look at
its data types, and its constructs in detail.