[Courses] [Perl] Part 1: Getting Started

Dan dan at cellectivity.com
Sat Apr 2 02:10:12 EST 2005


LinuxChix Perl Course Part 1: Getting Started

1) The Beginning
2) Hello World
3) Warnings and Strict Compilation
4) Exercises
5) Acknowledgements
6) Licensing


           ----------------------------------------

1) The Beginning

The first step to using Perl is installing it. If you're using a
Linux or BSD system you're ahead of the game, because it's rare these
days to find an open-source installation without Perl. If you're
running Windows, you may need to download Perl, and www.perl.org is a
good place to start.

           ----------------------------------------

2) Hello World

Since we're not big on originality, we're going to start with a Hello
World program:

   print "Hello, world!\n";

To execute the program, write it in a file (say, hello.pl) and
execute the following on the command line:

   perl hello.pl

Now let's make the program a little more interesting:

   my $who = "world";
   print "Hello, $who!\n";

Finally, let's add some comments. Comments can be provided using the
"#" character, which is often called a hash or pound sign. (I believe
the correct term is "octothorp".) The comment begins at the "#" and
ends at the end of the line.

   # This is a Hello World program in Perl.
   my $who;                  # Declare variable.
   $who = "world";           # Assign variable.
   print "Hello, $who!\n";   # Print result.

           ----------------------------------------

3) Warnings and Strict Compilation

Although the above program is a valid, working progam, I want to
introduce two tools that will save you hours of debugging later on.

The first tool is strict compilation. This requires you, among other
things, to declare each variable before you use it. This will alert
you of misspelled variable names. To use strict compilation, begin
the program with "use strict;". (We'll see an example below.)

The second tool is warnings. This will cause the interpreter to
mention probable logic errors as the program executes. For example,
the statement "$a = $b + c;" is syntactically correct in Perl, but
you probably wanted a dollar sign in front of the "c". If you have
warnings turned on, Perl will flag this mistake.

To turn on warnings, you can invoke the "-w" flag on the command
line:

   perl -w hello.pl

However, this requires the user to include the flag every time she
executes the program. A better solution is to use a "shebang" line at
the beginning of the program:

   #!/usr/bin/perl -w

The perl interpreter will honor the "shebang" line even if the "-w"
flag is not invoked on the command line.

Notice that the shebang line is special in two ways:
a) It starts with a hash but is not a comment.
b) It doesn't end with a semicolon.

If you're not familiar with shebang lines, feel free to post a
question to the list (this one or the techtalk list).

An additional benefit of the "shebang" line is that it allows you to
execute the script without typing "perl" on the command line, just as
if it were a shell script or a compiled program. (Don't forget to set
execution permissions.) However, it also means that we have to know
where on the filesystem the Perl interpreter is located. We can do
this by typing "which perl", or a similar command, at the command
line.

So our Hello World program becomes:

   #!/usr/bin/perl -w
   use strict;
   my $who;                  # Declare variable.
   $who = "world";           # Assign variable.
   print "Hello, $who!\n";   # Print result.

Experienced Perl programmers may sometimes turn off these safeguards,
but it's not recommended for beginners.

           ----------------------------------------

4) Exercises

a) In the Hello World program, what does the "\n" mean? If you don't
know, try moving it around inside the string and see what result it
gives.

b) Use the man pages to find out what "Perl" stands for.

           ----------------------------------------

5) Acknowledgements

A big thank you to Jacinta Richardson for suggestions and
proofreading. More advanced Perl users might want to check out the
free material from Perl Training Australia
<http://www.perltraining.com.au/>, which she is a part of.

Other contributors include Meryll Larkin.

           ----------------------------------------

6) Licensing

This course (i.e., all parts of it) is copyright 2003-2005 by Dan
Richter and Alice Wood, and is released under the same license as
Perl itself (Artistic License or GPL, your choice). This is the
license of choice to make it easy for other people to integrate your
Perl code/documentation into their own projects. It is not generally
used in projects unrelated to Perl.


More information about the Courses mailing list