PrevIndexNext
Introduction
These notes were put together to help teach a class in Introductory Perl.
They are not a substitute for a good book.
The order of topics here is different than in the books.
(Each book has a radically different ordering!)
The books are useful to see an alternate perspective - useful if you
don't quite understand the concept as presented here and in class.
This book is especially recommended:
"Learning Perl"
by Randal Schwartz, et al, published by O'Reilly,
O'Reilly has been the supreme source of Perl documentation for decades.
Another highly recommended book is "Perl Best Practices" by Damian Conway
published by O'Reilly. This book contains more advanced topics
than are covered in this tutorial but it is an excellent book to have
nonetheless.
In 1987 Perl version 1 was written by Larry Wall.
Perl version 5 came in 1996 - it was a complete rewrite
and was a revolutionary quantum leap.
Perl has its roots in Unix - many things in Perl were inherited
from sh, awk, sed, and C.
It is now a very portable language running on almost
every architecture including win32, linux, mac, vms.
Perl is supported by a world-wide community of thousands.
The Perl community and culture is permeated by a sense of
creativity, sharing, and openness.
It has always been free and open source.
There are many books, web sites, newsgroups, conferences, and mailing lists.
The main web site for information about Perl
is www.perl.com.
Perl is not really an interpreted language
even though there is no intermediate compiled form that is saved.
It is compiled EACH time before it is run.
It is amazingly fast nonetheless.
These notes were written in 2000 for a course I taught at a Junior College.
Today (2019) things have changed (of course) but Perl
is still used in many large corporations.
Python and Ruby and PHP have their market share and vibrant communities,
as well, but Perl was first - it is the oldest 'cousin' of these
other languages. Perl could be called a 'legacy' language ... but note:
the definition of a legacy program is 'one that works'!
QuickStart
Using whatever text editor you are most comfortable with
create a file named 'hello' with this in it:
print "hello, world\n";
This is a complete perl program that prints the words
"hello, world" followed by a newline.
At a command prompt enter this:
% perl hello (on Unix)
or
C:> perl hello (on MS Windows)
It should print:
hello, world
If you don't have Perl installed on your system you'll
get an error above. Ask either your system administrator
or your teacher to help you get it installed. Have them click
here
for a web site where you can get Perl. I don't explain how to
do this here because it is very system dependent. The aim here
is to make a quick start at learning Perl - not to learn how
to install Perl!
This class will consist of an introduction
to the various language elements of Perl followed by
examples of their use. Exercises will give you
an opportunity to try them out. Trying and Doing
is much better than Just Listening for learning
something new. Basically you will be editing
and running, editing and running - over and over.
It makes sense to optimize this repetitive cycle.
Tips on Unix
To make a Perl program executable:
% chmod +x hello
and insert a first line of:
#!/usr/bin/perl
then you can execute it with:
% hello
(This is assuming that the current directory is in your $PATH).
In the korn and bash shells, you can make aliases (probably in csh as well...):
alias e="vi hello"
alias x="hello"
then you can simply do:
x
e
x
much better!
There is, of course, the command history of csh (or bash or ksh):
!v
!h
!v
!h
but this is more trouble.
The absolute quickest way to do development in Perl
is to use a special feature of vi (and likely something similar for emacs).
In vi there is the map command:
:map X :w!^v^m:!clear; perl %^v^m
The circumflex above is for indicating a Control key.
The ^v means control-v.
To get an actual control-m in the map sequence
you have to precede it by control-v (v stands for verbatim).
With this map in place you can simply hit the X key
and vi will save and execute the current file!
This is optimal for quick development and testing. This can
be put in the $HOME/.exrc file (or .vimrc) so it is always there for you.
The above map uses X because it is not often used
in normal vi usage. It is also nicely mnemonic.
Here are two other maps that you may find useful:
:map K :'a,.w! /tmp/t^v^m
:map V :.r /tmp/t^v^m
With these in place you can easily copy and paste multiple
lines from one file to another. Mark a beginning line
with an 'a', move to the ending line and hit K.
This will copy the range of lines to /tmp/t.
Open another file and move to the line after which
you wish to copy the lines. Hit V and the lines
are inserted from /tmp/t. This ability IS part of vi with
named buffers but this entails lots of "typing in the dark".
Tips On MS Windows
On Win32 systems notepad is the simplest text editor but it is not really
for programming. There is also BBEdit, TextPad, Lemmy, Vim, Emacs,
UltraEdit, and many others. Choose one and get good at it!
Try using the keyboard instead of the mouse.
That mouse can really slow you down! We are doing two very simple
things here - editing and running. No need for the mouse.
Use these things to speed your process:
- Use Alt-F and then 'S' to save the file in NotePad
without exiting the editor.
Perhaps just Control-S will work.
- Use Alt-Tab to switch between the editor session and a DOS command prompt.
- Use the command history in the DOS window to reinvoke "perl hello".
You may need to use "doskey" to turn the history mechanism on.
If you name the file with a ".pl" extension then
you can simply double click on the file to execute it.
Note that if there are syntax errors or if the program
does not pause at the end, then the DOS command window
will vanish as soon as it appears - not very helpful!
For this reason it is easier to use the DOS window
command prompt for invoking Perl programs.
Exercises
Make the simplest of perl programs and invoke it two different ways.
Modify and run it several times in an efficient manner.
PrevIndexNext