Laboratory Week 2
Learning Objective:
Getting familiar with:
- using ActivePerl to execute scripts in MS-Windows,
- debugging using ptkdb, and
- looking up the standard Perl documentation.
You should also refresh your memory about basic Perl syntax.
You may use this set-up to develop all your client-side scripts in your assignments. You may also use it to develop some (not all) parts of your server scripts.
Access to Software:
In the lab, we will be using ActiveState's ActivePerl for Win32. It is a Perl package which consists of (among many things) a Perl interpreter which allows you to run Perl scripts in an DOS command line under Microsoft Windows (Win95 onwards).
Internal students may access ActivePerl in lab SC2.31.
External students may download the required software and install it on your own machine. Internal students may do the same, but are encouraged to still attend labs for supervision when doing the exercises below.
Exercises:
Copy the file C:\Perl\Eg\example.pl to the directory C:\Windows\Temp. Start a MS-DOS prompt, and go to the directory C:\Windows\Temp, and execute the script example.pl by typing the command "
perl example.pl". Edit the example.pl file (using Notepad or whatever editor you wish to use) so that it prints your name.Download the script in simple-pl.txt, which consists of a subroutine and a loop (rename the file to simple.pl if you wish). The script is suppose to print the line "The numbers are 0-1-2-3", but has a bug in it. Debug the script using the debugger ptkdb (stands for Perl Tk Debugger - Tk is a graphical toolkit commonly used to built GUIs for Perl scripts). You invoke the debugger by using it as an option for Perl, by typing "
perl -d:ptkdb simple.pl" at the MS-DOS command line while in the same directory as simple.pl. The debugger should start up with the script loaded in a viewable windows. Work out how to do the following in the debugger:
- Run the whole script
- Run to where the cursor currently is
- Set a break-point to stop the script
- Run the script one line at a time, stepping into the code for subroutines
- Run the script one line at a time, stepping over subroutines (ie. executing the subroutines without stepping into their code)
- Return from the current subroutine (ie. finish executing all the code in the current subroutine, and move back to where it was called)
Can you find the bug? (Comment: the bug in this example is so simple you should be able to figure it out even without using a debugger).
Download some example CGI scripts (the
*-cgi.txtfiles - rename them to*.cgiif you wish) and run them through the debugger. To debug CGI script which accepts parameters sent by HTML forms, you can supply the required parameters using the command line. For example, if you have a script myscript.cgi accepting form elements named "Username" and "Password", you can type the commandperl -d:ptkdb myscript.cgi Username="Hiew" Password="abcd_efg".
- Using the installed Perl documentation (which is standard for all versions of Perl on any system, on any web-site, anywhere in the world), look up the following:
- Perl language basic syntax descriptions (such as
forloops,ifstatements, etc)- Syntax for defining and calling subroutines.
- Details of Perl data types (such as scalars, arrays, hashes, etc)
- Example complex Perl data structures (such as arrays of arrays, arrays of hashes, etc)
- Description of Perl built-in functions (such as
push,delete, etc)- Command line options when executing a script on the command line.
You will probably need to look up items in these categories many times as you go into your programming exercises and your assignments.
Note: The reason why all the example scripts linked to this tutorial has a .txt extension instead of their proper .pl or .cgi extension is because they reside on the b336 web site. The apache server serving www.it.murdoch.edu.au will treat request for .pl or .cgi as request to execute the scripts rather than to return the source code. You will get to deal with this when you manage your own apache server next week.
Document author: H.L.
Hiew, Unit Coordinator
Last Modified: Sunday, 25-Feb-2001 23:16:17 MST
Disclaimer & Copyright Notice © 2001 Murdoch
University