Murdoch University LogoB211 Advanced Internet Computing

Laboratory Week 8 - Writing to a file in Perl


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:

Exercises:

In previous weeks' exercises, you tried out scripts which can read data on a server to do interesting processing (eg. the search engine example). The data are stored in files and you read from the files to get the data.

At different stages, you may want to allow your users to actually put data onto the server (eg. adding new lines to the search engine file). To do so, you will have to write HTML or CGI scripts to control how that happens. Try the following exercises.

  1. Retrieve the scripts for exercises 3-5 in week 7's lab. Modify the scripts so that instead of printing the results to the browser, the results are printed into a file.

    You can check whether it has put the correct data into the file by using your FTP program to download the file and view it.

  2. Logging information: Modify the example login scripts from week 6's lab so that whatever login attempt the script receives, the username and password are appended to a log file. Logging new attempts should not overwrite existing ones already archived.

  3. Allowing users to add data: Creating an online Guest Book.
    1. Write a HTML page with a form containing three text-boxes named "Name", "Date" and "Comment".
    2. Write a CGI script that reads the 3 parameters in (i), and append the data into a file, separating the 3 fields with a semicolon. Put the script onto your cgi-bin directory on red. Change the action field in your form in (i) so that it points to this script. Access the form and test it.
    3. Implement another CGI script which reads from the file created by the script in (ii), that sends the content in the file back to the browser as plain text. Put the script onto your cgi-bin directory on red.
    4. Add your page in (i) a text hyperlink "View past comments". The URL of the hyperlink should be the new script created in (iii). Access the page and test it.
    5. Modify your script in (iii) so that the results are send back to the browser properly formatted in HTML instead of plain text. Access the orginal page in (i) and test it.

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: Friday, 14-Sep-2001 13:36:58 MST
Disclaimer & Copyright Notice © 2001 Murdoch University