|
Perl is structurally
similar to Pascal and C, so if you've got experience programming
in these languages, Perl should be fairly simple to pick up.
There are small differences that may cause problems if you're not paying
attention, though.
A list of some of these differences can be found at http://language.perl.com/newdocs/pod/perltrap.html,
along with advice for programmers coming to Perl from other languages.
Mostly, you'll be able to pick out the changes from the lessons that
follow, though.
Perl, like most
languages, is made up of statements. These are instructions that manipulate
data,
control the program flow, and handle input and output. Typically, there
is one statement per line.
Each line in perl must end with a semicolon. This is so that the interpreter
can differentiate
between commands. Whitespace (spaces, tabs, blank lines) is ignored,
and for this reason,
use of whitespace to increase readability is recommended. Anything from
a '#' character to the
end of a line is treated as a comment and also ignored. We recommend
commenting one's code generously.
Note: There is no syntax in perl for multi-line comments,
like there is in C ('/*' and '*/'). Usage of '#' is equivalent to the
C++ usage of '//'.
How to Start a Perl Program
Every Perl program
begins with a line resembling '#!/usr/bin/perl', where '/usr/bin/perl'
should
be replaced with the actual path to the perl interpreter on your system.
On a system where the interpreter is located in /usr/local/bin, the
first line should be '#!/usr/local/bin/perl'.
If you're not sure, consult your system administrator.
Perl Files
Perl files generally
have the extension '.pl', for obvious reasons. For CGI programs, though,
the extension '.cgi' may be used in order to tell the WWW server software
that the file is
meant to be executed, rather than sent to the browser directly. On Unix-like
systems,
make sure the file is set +x. This usually translates into chmod 755.
The line from ls -l should look
something like this:
-rwxr-xr-x
1 user group 919 Aug 7 18:15 hello.pl
Example: hello.pl
Get This Sample
#!/usr/bin/perl # hello.pl
# This is the Generic Perl Script. It contains only one line of "real" code,
# but really, that's all that's needed. In fact, you could run a perl script
# with nothing but the first line, pointing to the perl interpreter, and it
# would return no errors. It wouldn't do anything terribly useful, though.
# Note we can say whatever we want on lines beginning with #.
# Even if we put "real" code, like the next line, it won't be executed.
# print "I just wanted to say";
# This is useful for temporarily disabling portions of a program for
# troubleshooting purposes. print "hello, world!\n"; # This is an inline comment. It's useful for # explaining the purpose of a given line.
# Note that you don't *have* to indent all
# the following comment lines (if there's more
# than one), but this is a case where using
# whitespace to improve readability is a good idea.
|