Home | Exercises | Solutions | Next

 

A Shortcut to Perl

Erik Tjong Kim Sang, Jakub Zavrel, Guy De Pauw and Walter Daelemans
CNTS - Computational Linguistics, University of Antwerp
http://lcg-www.uia.ac.be/~erikt/perl/


This text is part of the lecture notes for a Perl course taught at CNTS - Computational Linguistics at the University of Antwerp.

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 = 1976;
   $currentYear = 2000;
   $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 1976 in the variable $yearOfBirth. The second one fills $currentYear with the value 2000. 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 (18%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 = <>;
   chomp($yearOfBirth);
   print "Your age is ",2000-$yearOfBirth,".\n";

This program contains five lines. The first line starts with a hash mark (#) and this means that it is a comment. The program starts by printing a line on the screen. Then it reads a line (<>) 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 2000. It only uses the programming constructs discussed in this section which makes it look a little bit strange.

   # example.01: compute the day of the week
   # usage:      example.01
   # note:       the program will only work correctly for the year
   #             2000 and other leap years which start on Saturday
   # 2000-02-03 erikt@uia.ua.ac.be
   
   $dateNbr = 0;
   
   # determine month
   print "Answer the next questionaire with 1 (yes) or 0 (no).\n";
   print "Is the day after January 31? ";
   $answer = <>;
   chomp($answer);
   $dateNbr = $dateNbr+$answer*31;
   print "Is the day after February 29? ";
   $answer = <>;
   chomp($answer);
   $dateNbr = $dateNbr+$answer*29;
   print "Is the day after March 31? ";
   $answer = <>;
   chomp($answer);
   $dateNbr = $dateNbr+$answer*31;
   print "Is the day after April 30? ";
   $answer = <>;
   chomp($answer);
   $dateNbr = $dateNbr+$answer*30;
   print "Is the day after May 31? ";
   $answer = <>;
   chomp($answer);
   $dateNbr = $dateNbr+$answer*31;
   print "Is the day after June 30? ";
   $answer = <>;
   chomp($answer);
   $dateNbr = $dateNbr+$answer*30;
   print "Is the day after July 31? ";
   $answer = <>;
   chomp($answer);
   $dateNbr = $dateNbr+$answer*31;
   print "Is the day after August 31? ";
   $answer = <>;
   chomp($answer);
   $dateNbr = $dateNbr+$answer*31;
   print "Is the day after September 30? ";
   $answer = <>;
   chomp($answer);
   $dateNbr = $dateNbr+$answer*30;
   print "Is the day after October 31? ";
   $answer = <>;
   chomp($answer);
   $dateNbr = $dateNbr+$answer*31;
   print "Is the day after November 30? ";
   $answer = <>;
   chomp($answer);
   $dateNbr = $dateNbr+$answer*30;
   print "\n";
   
   # read day of month
   print "Please enter the day of the month: ";
   $answer = <>;
   chomp($answer);
   $dateNbr = $dateNbr+$answer;
   print "\n";
   
   # adjust for January 1 being a Saturday
   $dateNbr = $dateNbr+4;
   
   # 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";
   
   # exit
   exit(0);


Home | Exercises | Solutions | Next
Last update: February 07, 2000. erikt@uia.ua.ac.be