Home | Exercises | PDF slides | Next

 

Perl 2007: Lesson 1


This text is part of the lecture notes for a programming course taught at University of Tilburg, The Netherlands.

1. Variables and number processing

This section presents an introduction to Perl by presenting its variable structures, and operators and functions which can be used for processing numbers.

1.1. Variables

A variable is a name of a place where some information is stored. For example:

   $yearOfBirth = 1983;
   $currentYear = 2007;
   $age = $currentYear-$yearOfBirth;
   print $age;

This little Perl program contains three variables: $yearOfBirth, $currentYear and $age. Each line contains one command. The first command puts the value 1983 in the variable $yearOfBirth. The second one fills $currentYear with the value 2007. The third command computes a value for $age and the final command prints this value at the screen. Note that the commands are separated by semicolons.

The variables in the example program can be identified as such because their names start with a dollar ($). Perl uses different prefix characters for structure names in programs. Here is an overview:

Having special character prefixes in front of each name makes possible using the same name for different objects, for example: $age and @age are different objects. In this section we will only use variables containing scalar values. The other structures will be explained in later sections.

1.2. Operations on numbers

The program in the previous subsection contains an example of a variable being subtracted from another. The minus sign is called an arithmetic operator. Perl contains the following arithmetic operators:

The first four produce the obvious. The fifth results in the remainder of the first number divided by the second (8%5=3). The final operator raises the first number to the power of the second (2**3=8).

Apart from these operators, Perl contains some built-in arithmetic functions. Some of these are mentioned in the following list:

Almost any combination of the arithmetic operators and functions can be used. For example: $x = abs(rand()+1)+int(sqrt(4)/2); is a valid Perl command.

1.3. Input and output

The program in the first subsection contains an example of the output function print. We will use this function for writing lines at the screen. Here is another example:

   # age calculator
   print "Please enter your birth year ";
   $yearOfBirth = <STDIN>;
   chomp($yearOfBirth);
   print "Your age is ",2007-$yearOfBirth,".\n";

This program contains five lines. The first line starts with a hash mark (#) and this means that it is a comment which will be ignored during runtime. The program starts by printing a line on the screen. Then it reads a line from the standard input, usually the keyboard, (<STDIN>) and puts the result in the variable $yearOfBirth. Here we encounter a problem: the line contains both the number entered by the user and a code for the return key (newline) pressed after entering the number. We can use the function chomp() for removing this code. After that, the program prints the assumed age of the user. Note that print can display several strings after each other when they are presented as arguments separated by comma's. The code \n in the final string means newline.

1.4. Programming example

Here is a larger program for computing the weekday of dates in the year 2007. It only uses the programming constructs discussed in this section which makes it look a little bit strange.

   # example.03: compute the day of the week
   # usage:      perl example.03
   # note:       the program will only work correctly for the year
   #             2007 and other non-leap years which start on a Monday
   # 20000203 erikt@uia.ua.ac.be
   # 20070821 erikt@science.uva.nl
   
   $dateNbr = 6; # December 31, 2006 is a Sunday
   
   # determine month
   print "Answer the next questionaire with 1 (yes) or 0 (no).\n";
   print "Is the day after January 31? ";
   $answer = <STDIN>;
   chomp($answer);
   $dateNbr = $dateNbr+$answer*31;
   print "Is the day after February 28? ";
   $answer = <STDIN>;
   chomp($answer);
   $dateNbr = $dateNbr+$answer*28;
   print "Is the day after March 31? ";
   $answer = <STDIN>;
   chomp($answer);
   $dateNbr = $dateNbr+$answer*31;
   print "Is the day after April 30? ";
   $answer = <STDIN>;
   chomp($answer);
   $dateNbr = $dateNbr+$answer*30;
   print "Is the day after May 31? ";
   $answer = <STDIN>;
   chomp($answer);
   $dateNbr = $dateNbr+$answer*31;
   print "Is the day after June 30? ";
   $answer = <STDIN>;
   chomp($answer);
   $dateNbr = $dateNbr+$answer*30;
   print "Is the day after July 31? ";
   $answer = <STDIN>;
   chomp($answer);
   $dateNbr = $dateNbr+$answer*31;
   print "Is the day after August 31? ";
   $answer = <STDIN>;
   chomp($answer);
   $dateNbr = $dateNbr+$answer*31;
   print "Is the day after September 30? ";
   $answer = <STDIN>;
   chomp($answer);
   $dateNbr = $dateNbr+$answer*30;
   print "Is the day after October 31? ";
   $answer = <STDIN>;
   chomp($answer);
   $dateNbr = $dateNbr+$answer*31;
   print "Is the day after November 30? ";
   $answer = <STDIN>;
   chomp($answer);
   $dateNbr = $dateNbr+$answer*30;
   print "\n";
   
   # read day of month
   print "Please enter the day of the month: ";
   $answer = <STDIN>;
   chomp($answer);
   $dateNbr = $dateNbr+$answer;
   print "\n";
   
   # show answer
   print "In the answer the following table has been used:\n";
   print "0 = Monday\n";
   print "1 = Tuesday\n";
   print "2 = Wednesday\n";
   print "3 = Thursday\n";
   print "4 = Friday\n";
   print "5 = Saturday\n";
   print "6 = Sunday\n";
   print "\n";
   print "The week day number of your date is "; 
   print $dateNbr%7;
   print "\n";


Home | Exercises | PDF slides | Next
Last update: April 02, 2008. erikt(at)science.uva.nl