Perl examples
You can download these examples, creating files such as "example.pl".
The first line, the #! line, will need to be changed to contain the
correct path for your Perl interpreter (on Windows, this only really
matters for Perl scripts that will be run as CGI programs). Unix/Linux
users will probably find their Perl in /usr/bin/perl, but check with
the which perl command. The typical Windows install
will have the Perl interpereter in C:\Perl\bin".
needing a first line like #!/Perl/bin/Perl5.6.1 (your
Perl version may differ). Unix/Linux users can set execute permissions
on a Perl script (chmod +x example.pl) and then
just "execute" a Perl file (enter the file's name as if it were a Unix command).
Windows users should open a "Command Prompt" window (run cmd.exe),
then check that the Perl interpreter is on their PATH. (You
get the current settings for PATH with the command
set PATH; if you need to add the Perl interpreter then
the command will be something like set PATH=%C:\Perl\bin\;%PATH%).
Once the PATH is correct you can run progams by
invoking the perl interpreter, e.g. perl example.pl.
Examples include:
These fragments are:
qw and so producing
an incorrect list) and then using various forms of for loop
to print list elements;These fragments are:
This fragment is intended to let you experiment with the definition of regular expressions patterns, testing a defined pattern with different input strings.
The directory listing example program is available. This works fine on Windows, try "C:\Perl" as a directory name.
The laboratory class management code is also
included. This one will take a little setting up to get to work and is
limited to Unix/Linux as it is invoking shell tasks via a system
call or backticks construct.
The Perl HTTP minimal client may work and let you grab pages, but it won't find its way past a proxy server.
The drivers and datasources example lets you identify the database drivers that you have installed and then lets you find the datasources that these drivers can access. For example, on a Windows machine you should find an ODBC driver; and this should let you reach any datasource that you have defined with the ODBC data source control panel.
The E-Pal example has a simple program for adding personal records to a database of people interested in finding email-penpals, and for searching the contents of the database. Users define themselves as male, female, or "eperson", and specify the type of the desired penpal (male, female, eperson, or any). Users must also specify five interests from a list of about one hundred. The search will retrieve any records where there is an interest in common and the type preferences of both potential penpals are satisfied. The user interface is poor; as a later exercise you can rework the code as a CGI example and make use of a web page interface.
Enter command (add,search, list (interests), quit): add Your type : MALE Your desire : ANY Enter five personal interests : Cars Jazz Movies Skiing Snowboarding Your email address : Jason Enter command (add,search, list (interests), quit): add Your type : FEMALE Your desire : ANY Enter five personal interests : Aerobics ClassicalMusic Movies Painting Skiing Your email address : Angela Enter command (add,search, list (interests), quit): search Your type : FEMALE Your desire : ANY Enter five personal interests : Aerobics ClassicalMusic Movies Painting Skiing Contact mail joe: Common interests: Aerobics Contact mail fran: Common interests: ClassicalMusic Contact mail tom: Common interests: Aerobics Contact mail rachel: Common interests: ClassicalMusic Contact mail Jason: Common interests: Movies Skiing Contact mail Angela: Common interests: Aerobics ClassicalMusic Movies Painting Skiing Enter command (add,search, list (interests), quit): quit
The database for this example is just a single table (SQL definition). The program can be made to work if you create the database, modify the program so that it uses the correct database URL, and change the user-name and password. A Microsoft Access file is available in compressed form; Windows users should be able to install it somewhere and then use the Control Panel "Data Sources" to create a datasource named "epalfinder" that references this database. (The code supplied uses a DBD-ODBC connection.)
You need to have your Apache running first. You should have got this to work earlier.
The first example is the one with the trivial form with a radio-button choice that feeds data to a program that prints nursery rhyme responses.
The form is:
<html><head><title>Test page</title></head> <body> <form method=post action="/cgi-bin/form1.cgi"> What are you? <br /> <input type=radio name=sex value=Boy>Boy <input type=radio name=sex value=Girl>Girl <input type=radio name=sex value=Unsure checked>Unsure <br /> <input type=submit> </form></body></html>
This should be installed in the htdocs directory as form1.html.
The Perl CGI program should be installed in the cgi-bin
directory a form1.cgi. (The file extension doesn't matter, so long as the
file name is the same as in the HTML form. All files in the cgi-bin directory are
treated as CGI scripts. Later you may want more elaborate configurations with
CGI programs permitted in other directories; then the file extension, like cgi
as used here, should be something that is defined as being handled as a cgi script (an
AddHandler directive in httpd.conf file). The #! line will need to be edited
to specify the pathname of your Perl (the code is for a Windows Apache - ActivePerl
set up using their default directories).
Once both files are installed, and access permissions are set on Unix/Linux, you should be able to run this little test by pointing a browser at http://localhost/form1.html (assuming Windows Apache, for Linux you would have to specify the port as 8080).
The first version of the form handling program uses just standard Perl constructs with
most of the HTML code as "here" strings. Another version
uses the CGI module (this should be part of your Perl release, you shouldn't
have to download and install it). In this version, the various functions defined in the
CGI module are used to format headers, paragraphs and so forth. It is a matter of personal
preference. I actually prefer the naive approach with the "here" strings. They
make it easier for your web designer colleague to change a page layout without damaging code.
Finally, there is the epal example. This is a web-based version of the earlier DBI database example. The form page allows web users to add their details to your database or search the database for email pen-pals. The CGI processing program checks the request (possibly returning a fixed error response page) and then does the add or search operation. (Obviously, you have to edit database details if you try to set this up on your own system; the example code worked with ODBC and the Microsoft Access database used in the earlier example. The Access database does not have to be in the htdocs part of your file space. Access wouldn't withstand multiple concurrent access from many copies of the CGI program - but it is unlikely that you email pen pal site would be under that kind of load.)