IntroductionThese 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.Each of these books are highly recommended: Learning Perl by Randal Schwartz published by O'Reilly, Beginning Perl by Simon Cousins published by Wrox, and Elements of Programming in Perl by Andrew Johnson published by Manning. "Learning Perl" is the one recommended for people with some prior programming experience. For those new to programming, "Elements of Programming in Perl" is probably better. There are page references in the books interspersed throughout these notes. The references will be color coded - Learning Perl in blue, Beginning Perl in green and Elements of Programming in Perl in red. 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 it 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.
QuickStartUsing whatever text editor you are most comfortable with create a file named 'hello' with this in it: This is a complete perl program that prints the words "hello, world" followed by a newline.print "hello, world\n"; At a command prompt enter this: It should print:% perl hello (on Unix) or C:> perl hello (on MS Windows) 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!hello, world 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 UnixTo make a Perl program executable:and insert a first line of:% chmod +x hello then you can execute it with:#!/usr/local/bin/perl (This is assuming that the current directory is in your $PATH).% hello In the korn and bash shells, you can make aliases (probably in csh as well...): then you can simply do:alias e="vi hello" alias x="hello" e x e xmuch better! There is, of course, the command history of csh (or bash or ksh): !v !h !v !hbut this is more trouble.
Perl and Vi - A Poor Man's IDEThe 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:!perl %^v^mThe 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. You can also do this: :map V :w!^v^m:!perl -c %^v^mfor checking for syntax errors (V stands for verify) without leaving vi. The above maps use X and V because these keys are not often used in normal vi usage. They are also nicely mnemonic. Tips On MS WindowsOn 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:
ExercisesMake the simplest of perl programs and invoke it two different ways.Modify and run it several times in an efficient manner. |