Writing Perl Statements
Every part of a Perl script (except comments) consists of a
statement. Statements:
- Contain one or more of the following expressions:
- A call to a function or subroutine--internal
or external.
- The use of a LValue and RValue. Since Perl
evaluates from the right side of a statment to the left
side of a statement (like "C"), we use the
"C" programmer's LValue (left-side
value) and RValue (right-side value) notation. The LValue
receives the result of the RValue.
- The use of a flow control term to handle the
order in which Perl executes your instructions. Perl
uses flow control statements similar to other
languages. Examples of flow control statements
include: for, while, and if/then/else.
- Exist anywhere in the file (except in comments).
Perl ignores all "whitespace" (spaces, tabs,
carraige returns, and line feeds).
- End with a semicolon (;). By using a semicolon, you
can group multiple statements on one line.
Perl gives you the freedom to write how you want. However, a good
coding style can help people who work on your code (including you) figure
out what they need to change.
In Perl, many people find it easy to write cryptic statements that
appear to accomplish a lot. However, cryptic statements typically
make it a nightmare for someone who needs to change the code.
Therefore, it makes sense to review and practice some basic coding
styles, which follow:
- Use meaningful variable names to make it clear how
and for what you plan to use the variable. This may mean
mixing variable name case such as inputFileHandle,
outputFileHandle, and outputString.
- Indent lines to reflect nesting loops to make it
easy for people to find mistakes.
- Call subroutines for things you do more than once
to make the main part of the program more readable and to
reduce the number of places you must fix mistakes.
- Document your code using who, what, where, when, and
why. By using this technique, you'll always know who to
ask if you need help debugging, what the routine does, where
in the overall system the routine belongs, when it needs a
good rewrite in comparison to the rest of the code, and why
it exists.
- Return meaningful values from subroutines. Even if
you think nobody would ever need to know the relative
success of your routine, assume they may. This reduce code
re-writes and improves error-checking in a program calling
your routing.
By following these coding guidelines, you should save your time and
the time of others. Note that the examples in this tutorial do not
adhere strictly to these guidelines because they serve a special
purpose.
These examples may seem simple, but they do illustrate the points of
this section. The example below shows the call to the Perl
"Print()" function:
# Example of a statement using the Perl Print() function.
print("Hello, world!");
This next example shows the assignment of the value "1" (a
RValue) to the variable $index (a LValue):
# Example of a statement using a LValue and RValue.
$index = 1;
Finally, we show you an example of a statement using the Perl
"if" program flow control statement:
# Example of a statement using program flow control.
if ($index eq 1) {
print "Index equals 1!\n";
}
Now that you know how to write comments and statements, you should
take the next big step of running a Perl program.