
| Contents |
|||||||||||||||||||||||||||||
| [an error occurred while processing this directive] |
1.1 A First Perl ProgramSo, 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 on through 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 username, and print out a message greeting the user by name.
#!/usr/bin/perl use strict; # @cc{important pragma} use warnings; # @cc{another important pragma} print "What is your username? "; # @cc{print out the question} my $username; # @cc{``declare'' the variable} $username = <STDIN>; # @cc{ask for the username} chomp($username); # @cc{remove ``new line''} print "Hello, $username.\n"; # @cc{print out the greeting} # @cc{Now we have said hello to our user} Let us examine this program line by line to ascertain its meaning. Some hand-waving will be necessary, since some of the concepts will not be presented until later. However, this code is simple enough that you need not yet understand completely what each line is doing.
The first line is how the program is identified as a Perl program. All
Perl programs should start with a line like In the lines that follow, halfway through each line, there is a `#' character. Everything from the `#' character 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 themselves. The last line of the program above is an example of that. Now, consider the code itself, ignoring everything that follows a `#' character. Notice that each line (ignoring comments) ends with a `;'. This is the way that you tell Perl that a statement 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 Perl.
The first line,
The second line is another pragma,
So, you might wonder why two separate pragmas are needed. The reason is
that they are enforced by Perl at different times. The
By contrast, the The third line is the first statement of the program the performs an action directly. It is a call to Perl's built-in @builtin{print} function. In this case, it is taking a string (enclosed in double quotes) as its argument, and sending that string to the standard output, which is, by default, the terminal, window, or console from which the program is run.
The next line is a variable declaration. When in @module{strict}
mode (set by the
The next line,
The right hand side of the assignment is a construct that allows us to
get input from the keyboard, the default standard input. @fileh{STDIN}
is called a file handle that represents the standard input. We
will discuss more about file handles later. For now, just remember that
the construct Thus, at this point, we have the next line of the input (which is hopefully the username that we asked for), in the @scalar{$username} variable. Since we got the contents of @scalar{$username} from the 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 @scalar{$username} 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 The final statement is another @builtin{print} statement. It uses the value of the @scalar{$username} variable to greet the user with her name. Note that it is acceptable to use @scalar{$username} inside of the string to be printed, and the contents of that scalar are included. This ends our discussion of our small Perl program. Now that you have some idea of what Perl programs look like, we can begin to look at Perl, its data types, and its constructs in detail.
|