Perl Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 5.00

Class Variables and Object Variables

This section provides a tutorial example on how to define and use class variables and object variables. Class variables are stored at the class (package) level. Object variables are stored in the object. If an object is a reference of a hash variable, object variables are really hash keys and values.

Using Package Level Variables as Class Variables

In Perl, all package level variables are class variables. There is no private variable concept in Perl. All variables are publicly accessible in the same way as package variables.

Using Hash Entries to Store Object Variables

As you can see in the previous section, you can bless a reference of any "thing" into an object of a specific class.

The number and type of object variables you can use depend on the data type you select for the references:

  • If you use references of scalars, you can only have one object variable.
  • If you use references of arrays, you can have many un-named object variables.
  • If you use references of hashes, you can have many named object variables. Of course, hash is the best choice to host object variables.

In order to define object variables, we must create objects with references of the same data type for the same class. Using references of different data types for the same class will cause confusion to class and object methods.

Since objects are references, accessing object variables can be done in the same way as accessing references.

Here is a tutorial program to show you how to handle class variables and object variables:

#- ObjectVariableTest.pl
#- Copyright (c) 1999 by Dr. Herong Yang, http://www.herongyang.com/
#
package Account;
   $euroRate = 0.85;
sub print {
   my $this = shift;
   my $currency = shift;
   my $balance = $$this{"Balance"};
   $balance *= $euroRate if ($currency eq "EURO");
   print("Printing account...\n");
   print("   Name = ",$$this{"Name"},"\n");
   print("   Type = ",$$this{"Type"},"\n");
   print("   Balance = ",$balance,"\n");
}
sub deposit {
   my $this = shift;
   my $amount = shift;
   $$this{"Balance"} += $amount;
}
sub setEuroRate {
   my $class = shift;
   $euroRate = shift;
}
package main;
   %myAccount = {};
   $myAccount{"Name"} = "Herong Yang";
   $myAccount{"Type"} = "Checking";
   $myAccount{"Balance"} = 100.00;
   $myObj = \%myAccount;
   bless($myObj,Account);

   %hisAccount = {};
   $hisAccount{"Name"} = "Mike Clinton";
   $hisAccount{"Type"} = "Saving";
   $hisAccount{"Balance"} = 999.00;
   $hisObj = \%hisAccount;
   bless($hisObj,Account);

   print("\nTest 1:\n");
   $myObj->print();
   $hisObj->print();

   print("\nTest 2:\n");
   $myObj->deposit(-25.00);
   $hisObj->deposit(99.00);
   $myObj->print();
   $hisObj->print();

   print("\nTest 3:\n");
   $myObj->print("EURO");
   $hisObj->print("EURO");

   print("\nTest 4:\n");
   Account->setEuroRate(0.80);
   $myObj->print("EURO");
   $hisObj->print("EURO");
   exit;

Obviously, $euroRate is used as a class variable. Keys "Name", "Type" and "Balance" in the hash are used as object variables. Here is the output of the tutorial program:

Test 1:
Printing account...
   Name = Herong Yang
   Type = Checking
   Balance = 100
Printing account...
   Name = Mike Clinton
   Type = Saving
   Balance = 999

Test 2:
Printing account...
   Name = Herong Yang
   Type = Checking
   Balance = 75
Printing account...
   Name = Mike Clinton
   Type = Saving
   Balance = 1098

Test 3:
Printing account...
   Name = Herong Yang
   Type = Checking
   Balance = 63.75
Printing account...
   Name = Mike Clinton
   Type = Saving
   Balance = 933.3

Test 4:
Printing account...
   Name = Herong Yang
   Type = Checking
   Balance = 60
Printing account...
   Name = Mike Clinton
   Type = Saving
   Balance = 878.4

Sections in This Chapter

Basic Concepts of Classes and Objects

Invoking Package Subroutines as Class Methods

bless() - Converting References to Objects

Invoking Package Subroutines as Object Methods

Class Variables and Object Variables

new() Method - Creating Objects by the Class

CalendarClass.pm - A Perl Class Example

Dr. Herong Yang, updated in 2008
Class Variables and Object Variables