ICT336 title
Murdoch University logo

Laboratory Week 3 Part B - Introduction to Perl Programming


Learning Objective:

  1. Be familiar with basic Perl language syntax.

Required Reading:

Lectures: Week 2 lecture 2
Unit Reader 3: Programming in Perl 5

Access to Software:

In this lab, you will need access to a text editor to write Perl scripts, as well as a Perl interpreter to execute the scripts. You may use a ssh session to gryphon.murdoch.edu.au for this purpose. See required software and install it on your own machine off campus. Internal students may do the same, but are encouraged to still attend labs for supervision when doing the exercises below.

Instructions:

In the examples, the words in teletype bold like this are commands you type in a command line. The words in teletype like this is script code you put in a script file.

Writing and executing Perl script

Use any text editor to write scripts. The standard file extension for Perl scripts is ".pl". Once you have created the file, execute the script by using the "perl" command at the command line (on a unix shell or Windows command prompt). eg. if you create a file with file name "myscript.pl", you can execute the script by the command:

perl myscript.pl

If you are using a unix shell (such as on gryphon), you may execute the script directly by giving it executable permission. You have to ensure you have the path to the Perl interpreter in the shebang line (the first line) of the script. Eg.

chmod 750 myscript.pl
./myscript.pl

If you have the current directory "." in your path, you do not even need the "./" to identify the script location. Just do the following:

chmod 750 myscript.pl
myscript.pl

You only need to execute the "chmod" command once for each file. This permission will stay even if you change the file contents, or rename the file.

Reading Script Parameters

Scripts may be provided with parameters. Eg.

perl myscript.pl 123 porky

To read these script parameters, we can use the @ARGV array in the script, like so:

#!/usr/bin/perl -w
my ($num, $pig) = @ARGV ;
print "$num \n $pig \n" ;

Or like so:

#!/usr/bin/perl -w
my $num = $ARGV[0] ;
my $pig = $ARGV[1] ;
print "$num \n $pig \n" ;

You may also use the shift function, which operates on the @ARGV array when it is at the file scope (ie. at the top level of the script, when it is not in a subroutine or in control block). Eg.

#!/usr/bin/perl -w
my $num = shift ;
my $pig = shift ;
print "$num \n $pig \n" ;

which is exactly the same as

#!/usr/bin/perl -w
my $num = shift @ARGV ;
my $pig = shift @ARGV ;
print "$num \n $pig \n" ;

which is exactly the same as

#!/usr/bin/perl -w
my $num = shift(@ARGV) ;
my $pig = shift(@ARGV) ;
print "$num \n $pig \n" ;

Did I mention in the lectures that there are many ways to write the same code in Perl?

Keep the above in mind, as they all apply to how a subroutine reads its parameters as well. The only difference is that a subroutine operates on @_ instead of @ARGV.

Exercises:

  1. Create a script using one of the 5 example code sets above. Test the script by executing it and giving it different parameters.

  2. Create a script that takes ANY number of parameters. Your script should convert the characters in the script to uppercase, and print them out one parameter per line.

    [Hint: if your first thought on how to extract from the @ARGV array is to use a for or a while loop. Then you're not thinking like a Perl programmer.]

  3. Create a script that reads a text file and prints the contents of the file to the command line. Create your own text file to test the script.

  4. Create a script that reads a text file and prints only the lines in the file that contains the string "porky pig". Create your own text file to test the script.

  5. Create a script that reads a text file and prints only the lines in the file that contains the tag "<!-- ... -->", where "..." is any sequence of characters not containing the new-line character. Create your own text file to test the script.

  6. Create a script that reads a text file and substitute all tags of the form "<!-- ... -->" (where "..." is any sequence of characters not containing the new-line character), with a blank string. All other characters should be printed out as they appear in the file. Create your own text file to test the script. Make sure your script works when a particular line contains two or more of the specified tag.

  7. Create a script that contains a hash storing the relationship between two-character DNS top-level country domains, and the full country names. Eg. the "au" should be stored with "Australia", "nz" should be "New Zealand", "sg" should be "Singapore", etc. The script should read one two-character string as a parameter, and print out the corresponding full country name.

Assessable Tasks:

Exercises 2 and 6 above.

Internal students should demonstrate to your tutor by executing your scripts with your tutor present. This must be done in week 3 or week 4. No marks will be awarded if the work is demonstrated after that - your tutor have no discretionary power on this deadline.

External students should submit the scripts and sample runs as Assignment 3.

Perl References:

The purpose of this lab is only to get you familiar with Perl syntax. Although it covers some major components of Perl, it doesn't cover everything that is available from the lecture notes and the Unit Reader. I suggest you try some of the sample code given in Unit Reader 3 and experiment with other aspects of Perl.

There will be times when the material supplied in the lectures and Unit Reader is not sufficient to support what you want to do in your scripts. When that situation arises, you may refer to the following Perl language references:

H.L. Hiew
Unit Coordinator


Document author: H.L. Hiew, Unit Coordinator
Last Modified: Monday, 23-Feb-2004 01:22:11 MST
Disclaimer & Copyright Notice © 2004 Murdoch University
This document is relevant for semester 1, 2004 only