PERL variables are indicated by the $-sign:
Variables start with a letter after the $ and may not contain any blanks. Upper and lower case letters are treated differently, this means that $fred and $Fred are different variables. Variables can be of any length and may consist of upper and lower case letters, the underscore sign (_) and digits.
The names of variables should be defined with care. It is no good practice to call variables $a, $b, $a1, $gfH_7 or like. This makes a program hard to understand. It is better to use a name with a meaning, like $number_of_elements, $first_entry, $length_of_reading_frame etc. Of course such names mean more typing efford but they will make all the difference when you try to understand and modify your program a year or 5 years later. A program with short names compiles and runs almost as fast as a program with lengthy names, usually the difference in execution time will not be recognizable by the user, so you will increase the efficiency of your program only marginally by the use of cryptic designations. Usually the lengthiest part in programming is the development of the program and the removal of errors, not the actual running time.
Variables are defined and assigned the first time they
are used in the program.
$john = 17;
$fred = "cool";
This shows the two types of variables: $john will be a number and $fred is used as a string of characters. These types also decide what you can do with the variables: You may add/substract numbers or you may append strings. The two examples above are called assignment statements or simply statements. Every complete statement in PERL ends with a semicolon (;). This semicolon finalizes a statement and separates it from other statements. The variable may be thought of as a container that is filled with the value on the right side of the equal sign (=). So the variable called $john is filled with the integer number 17 and the variable $fred points to the string "cool". In the above statements it does not matter whether additional blanks are inserted between the name of the variables, the equal sign and the value. The statement:
is treated the same as
$john = 17 ;
There may be no blanks within the variable name of course. Adding blanks to your statements will nevertheless increase the readability in many cases.
Please note that if a hash-sign (#) is on a line, everthing following it will be ignored. In this fashion you add comments into your program. The first line is not completely comment since it is used atleast in UNIX environments to indicate the pathname of the perl program. The next line put the copyright into our program. print is a special command to start printing to the screen. The variable(s) after the print will be shown in the output of this program:Hello World!
A string, which is bordered by double quotes (") will simply be printed as it is. Since we have no means to add special characters to strings, they are preceded by a backslash (\). A "\n" will print a newline character, i.e. the next printed line will begin on a new line. You will find more special characters in the list of escape characters.
Any variable that appears for the first time in a program is "created", i.e. gets assigned a place in computer memory. The type of the variable can be any one of three different kinds: integer number, real number or string. Which type a variable is assigned will be determined by the right side of the first assignment statement. $john = 17; creates an integer variable, $john = 3.54; creates a real number variable and $john = "cool"; will create a string variable.
Variables that are used in a program without prior assignment will be assigned a default value. Integer variables will be assigned the value 0, real variables 0.0 and strings become an empty string "". This can be a problem when the names of variables are accidentally mispelled: $John instead of $john will create a new variable. To avoid this error it is recommended to add the line use strict; at the beginning of your script. Then each new variable has to be defined before use by a statement beginning with my. The above first PERL program will then look like this:#!/usr/local/bin/perl
Write a program that produces your timetable of this week. Please send your solution to me (email@example.com) and indicate if I can add it to this webpage with/without your name.
The timetable might look like this:+-------------------------------------------------------------------------------------+
Three solutions for this exercise have been returned: