An Introduction to Perl

 

Preliminaries

The following are some initial comments about Perl.  The goal of these are to give you a feeling for the language and whay you should care about learning it.

  1. Perl stands for Practical Extraction and Extraction Language.
  2. Perl has a very loyal following.  Some say a cult-like following.  There is a community of Perl programmers out ther with a very welcomming attitude to new comers.
  3. Perl was released on October 18, 1987 by Larry Wall.  It is freeware.
  4. Perl is derived from C, but you will see other influences including BASIC, awk (UNIX utility for pattern scanning.), sed (UNIX stream editor utility.), and the UNIX shell.
  5. It will be an easy language for us to learn given our background.  It might not be as easy to master.
  6. Its area of strength is manipulating text files and system processes.
  7. It is very concise.  There are amazing things that can be done in one line.  E.g. Reverse the order of the lines in a file.
  8. People have developed impressive applications quickly.  I have a friend who developed an entire trading system in Perl.
  9. It continues to be freeware with a community of people adding features.  Web applications and database applications have been developed in Perl.  Much of the functionality has been added through modules.  These are like libraries.
  10. The current version of Perl is version is 5.9.3.  It is free and can be downloaded.
  11. Perl was originally written for UNIX, but it has been ported to other system.
  12. Perl is an interpreted language.  It translates to a byte code which is interpreted.  So, one can expect it to run slower than C or C++.  But, unless the program is CPU bound, this might not be a factor.
  13. There are man pages for Perl.  Start with "man perl" and it will give you lots of pages to view.
  14. We will only discuss running Perl on UNIX.  There are slight differences running it on other OSs.
  15. We are using the book "Learning Perl" by Schwartz and Phoenix, published by O'Reilly.
  16. Perl is a good tool to quickly knock out an application.
  17. You can write extremely ugly code in Perl.
  18. Larry Wall must have hated to type.
  19. Perl has few limitations.  For example, there is no limit on the maximum length string.

 

Our First Program

To be in line with the rest of the world, our first program will display "Quack Quack"

#!/usr/bin/perl -w

# Ducks are good!

print "Quack Quack\n";
 

Note:

  1. #!/usr/bin/perl -w is interpreted by the shell as the path to the Perl interpreter.  The -w is one of the Perl flags to give us warnings if we are doing weird things.  There are other flags.  -t for trace may be useful
  2. It is good to use the .pl extension.  It lets emacs know that we are editing a Perl program.
  3. # indicates a comment.
  4. The print statement has the same format as the printf from C++.  If you don't remember it, I will fill you in as we go along.