Herong's Tutorial Notes on Perl - Part A
Dr. Herong Yang, Version 4.09

perlobj - Perl Objects

Part:   1  2  3  4   5 

(Continued from previous part...)

#- ObjectVariableTest.pl
#- Copyright (c) 1999 by Dr. Herong Yang
#
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:

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

Using new() Method to Create Objects

As you can see from the sample program, ObjectVariableTest.pl, objects are created and initiated in the calling package. This is not a safe approach, because the calling package could initialize the object with incorrect hash keys.

A better approach is for the class itself to offer a method to create objects and possiblly initialize them. This method is usualy called new().

So I improved my program by adding the new() method in the Account class. Notice that I have to create a reference to an anonymous hash in the new() method, so that each call of new() will get a reference (blessed into an object) of a new hash.

(Continued on next part...)

Part:   1  2  3  4   5 

Dr. Herong Yang, updated in 2006
Herong's Tutorial Notes on Perl - Part A - perlobj - Perl Objects