Murdoch University LogoB211 Advanced Internet Computing

Laboratory Week 7 - ActivePerl, Perl Subroutines, File Handling, String Manipulation


Learning Objectives:

Before doing the tutorial, please read:

Reference

At some stage after completing the instructions and exercises below, you should have a more careful look through the Web Programming textbook chapters 7, which gives much more details about Perl programming.

Keep in mind that there are many different ways to do things in Perl, and some of the techniques used in the textbook are slightly different to the ones in the lectures and the examples below. The reason I have not stuck to the textbook's approach only is because some of the examples are unecessarily complex for a beginners' course.

Software required:

Instructions:

  1. In your week 6 exercises, you learnt how to set-up and test Perl/CGI scripts on a live web server on red.murdoch.edu.au. This is important for others to make use of your scripts. But if you are working on your scripts in MS-Windows, it is a hassle to always have to upload your scripts to red before being able to test it. Also, the web server does return simple error messages such as for syntax errors for the scripts when the errors exist. This makes it very hard to debug and develop scripts.

  2. Fortunately, with ActivePerl available in MS-Windows, you can run your Perl/CGI scripts without having to upload them to red, and the Perl interpreter will also display any obvious errors it finds to you. To use ActivePerl on your MS-Windows machine to run a Perl/CGI script:
    1. Start-up an MS-DOS prompt. Go to the directory where your script is.
    2. Type in the command of the form
      c:\perl\bin\perl <name of script> <CGI parameters>
      .
      For example, to run and test the login1.cgi script from last week, we would type
      c:\perl\bin\perl login1.cgi name="test" passwd="test"

    If there are syntax errors with your script, your will get an error message appearing on the command line.

Exercises:

  1. Copy the example scripts you tried in week 6 to your local drive in MS-Windows. Use ActivePerl and an MS-DOS prompt to run them, and see if they behave as you expected. Put some deliberate mistakes into those scripts (eg, delete a bracket) and run them again. Learn to recognise the error messages.

  2. Retrieve the CGI script you created for exercise 5 from week 6. Modify the script so that you have a subroutine called NumberOfDays, which takes one parameter. The parameter should be a string corresponding to the name of the month, and the subroutine should return the number of days in that month as a return value. Change your script so that it calls this subroutine.
  3. Write a CGI script which reads from a file, and prints out the lines where the word "the" appears. Create your own text file to test your script with.
  4. Write a CGI script which reads from a file, and prints out the lines in the file but with the word "the" removed. Create your own text file to test your script with.
  5. Write a CGI script which reads from a file, and prints out the lines in the file but with all characters in upper case. Create your own text file to test your script with.

Remember to read the week's development on the Internet by scanning through relevant articles in the IT section of Tuesday's the Australian newspaper - use the online version or the physical copy in the University library. Get into the habit of keeping up with current developments. Test your knowledge of major developments by going through the Good News Week self-tests.

H.L. Hiew
Unit Coordinator


Document author: H.L. Hiew, Unit Coordinator
Last Modified: Sunday, 02-Sep-2001 03:53:52 MST
Disclaimer & Copyright Notice © 2001 Murdoch University