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:

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

Table of Contents

 About This Book

 Perl on Linux Systems

 ActivePerl on Windows Systems

 Data Types: Values and Variables

 Expressions, Operations and Simple Statements

 User Defined Subroutines

 Perl Built-in Debugger

 Name Spaces and Perl Module Files

 Symbolic (or Soft) References

 Hard References - Addresses of Memory Objects

Objects (or References) and Classes (or Packages)

 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

 Typeglob and Importing Identifiers from Other Packages

 String Built-in Functions and Performance

 File Handles and Data Input/Output

 Open Files in Binary Mode

 Open Directories and Read File Names

 File System Functions and Operations

 Converting Perl Script to Executable Binary

 Using DBM Database Files

 Using MySQL Database Server

 Socket Communication Over the Internet

 XML::Simple Module - XML Parser and Generator

 XML Communication Model

 SOAP::Lite - SOAP Server-Client Communication Module

 Perl Programs as IIS Server CGI Scripts

 CGI (Common Gateway Interface)

 XML-RPC - Remote Procedure Call with XML and HTTP

 RPC::XML - Perl Implementation of XML-RPC

 Integrating Perl with Apache Web Server

 CGI.pm Module for Building Web Pages

 LWP::UserAgent and Web Site Testing

 References

 PDF Printing Version