The Basics - Variables


As we have seen in all the languages we have studied thus far, variables are an important and sometimes confusing concept. In C++ and JAVA there are numerous types of variables, many of which are extremely similar. the only real difference is how the variables are stored, deep down in the implementation of the language. Most casual users of the big two languages know that int is for whole numbers, and float is for real numbers, but to make things confusing there is also a double which is the same thing as a float really. What about the addition of items such as long which can be used alone, or with other variable names? This is all very confusing, and in the long run, it doesn't really matter, unless you are really particular about such things.

Perl tries to insulate the user from such restrictions. Why should the programmer be burdened with information such as in what internal format a number will be stored with? In Perl, all the guess work has been taken out of variables. There are 3 basic kinds of variables in Perl:

All variable names in Perl can be up to 255 characters long, and can contain any letter, number, or underscore; no other punctuation is allowed. You must always remember to place the appropriate symbol at the start of the name ($ for scalar, @ for array, or % for hash). Declaring variables in Perl is not a requirement, as you can see from my examples. As I start to use the variable (initialize it), that is good enough for the interpreter. As we learn more about Perl, you will see that there are ways to declare variables to go along with different scopes.

Besides the three major ways of using variables, you also have the option to dynamically create and define your own variables. This is similar to JAVA and C++, and is one reason that Perl can now be classified as an Object Oriented Language.

The last variable concept we will discuss is the notion of a multi-dimensional array. There is no specific type for this, but you can easily create one using arrays, hashes, and scalars. Lets see an example:

      #!/usr/local/bin/perl -w


      @Robila = ("CMPT 183 ", "CMPT 109 ");


      %teachers_classes = (
      	      Deremer => ["CMPT 588 ", "CMPT 280 "],
      	      Koeller => ["CMPT 586 ", "CMPT 109 "],
	      Johnson => ["CMPT 372 "],
	      Bredlau => ["CMPT 583 ", "CMPT 112 ", "CMPT 109 "],
	      Boyno => ["CMPT 184 "],
      );


      # adding an array to the structure

      $teachers_classes{"Robila"} = [@Robila];


      foreach $teacher (keys %teachers_classes) {
        print $teacher, "\t", @{$teachers_classes{$teacher}}, "\n"
      }
      print "\n";


      # add another (but not as a stored array) to the structure

      $teachers_classes{Benham} = ["CMPT 576 ", "CMPT 109 "];

      foreach $teacher (keys %teachers_classes) {
        print $teacher, "\t", @{$teachers_classes{$teacher}}, "\n"
      }
      
The output of this little script looks like so:
 
      Bredlau CMPT 583 CMPT 112 CMPT 109
      Deremer CMPT 588 CMPT 280
      Johnson CMPT 372
      Robila  CMPT 183 CMPT 109
      Boyno   CMPT 184
      Koeller CMPT 586 CMPT 109
 
      Bredlau CMPT 583 CMPT 112 CMPT 109
      Deremer CMPT 588 CMPT 280
      Johnson CMPT 372
      Benham  CMPT 576 CMPT 109
      Robila  CMPT 183 CMPT 109
      Boyno   CMPT 184
      Koeller CMPT 586 CMPT 109

      
This is what is known as a 'Hash Of Arrays', mainly because we are storing array elements inside of a hash. It is possible to store the other 3 possibilities as well: 'Hash Of Hashes', 'Array Of Hashses', and 'Array Of Arrays' are all similar in nature.

With all of these building blocks, it is possible to create very accurate and complete data structures.


Compiling    <Variables>    Variables 2

Created By: Jason Zurawski
Last Modified: Feb. 29, 2004