new() Method - Creating Objects by the Class

This section provides a tutorial example on how to new() method to create objects inside the class. A typical new() method will create a reference of an anonymous hash with initial keys and values, then bless it to an object.

As you can see from the sample program,, 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 possibly initialize them. This method is usually 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.

#- Copyright (c) 1999 by Dr. Herong Yang,
package Account;
   $euroRate = 0.85;
sub new {
   my $class = shift;
   $this = {};
   $$this{"Name"} = shift;
   $$this{"Type"} = shift;
   $$this{"Balance"} = shift;
   return bless($this,$class);
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;
   $myObj = Account->new("Herong Yang","Checking",100.00);
   $hisObj = Account->new("Mike Clinton","Saving",999.00);

   print("\nTest 1:\n");

   print("\nTest 2:\n");

   print("\nTest 3:\n");

   print("\nTest 4:\n");

The output is the same as in the previous section, but now the main package is completely not aware how the objects are created, and what variables are there in each objects. Everything is hiding behind the objects.

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 - 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 Module for Building Web Pages

 LWP::UserAgent and Web Site Testing


 PDF Printing Version