Laboratory Week 8 - Writing to a file in Perl
Learning Objectives:
- Learn to store data into a file in Perl.
Before doing the tutorial, please read:
- Week 5 lectures on CGI programming and Perl file handling.
- Review what you learnt in the week 7 exercises.
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:
- This lab requires the use of ActivePerl, a SSH, an FTP program. The software is available in the internal labs. External students may download the required software from the unit's downloads page.
- This lab also requires a graphical web browser (like Netscape Navigator or Microsoft Internet Explorer), and a text editor (like Notepad).
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.
- 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.
- 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.
- Allowing users to add data: Creating an online Guest Book.
- Write a HTML page with a form containing three text-boxes named "Name", "Date" and "Comment".
- 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.
- 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.
- 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.
- 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