- A Perl Class Example

This section provides a tutorial example of a simple Perl class, CalendarClass with new() method, class variables, and some set*() and get*() object methods.

Here is another class,, that shows you how to define class methods, object methods and object creation method:

#- Copyright (c) 1999 by Dr. Herong Yang,
package CalendarClass;
sub BEGIN {
   $author = "Dr. Herong Yang";
   $version = "1.00";
   $info = "My first Perl class.";
sub new {
   my $this = shift;
   my $class = ref($this) || $this;
   my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$x) = localtime();
   my $self = {};
   $$self{"SEC"} = $sec;
   $$self{"MIN"} = $min;
   $$self{"HOUR"} = $hour;
   $$self{"MDAY"} = $mday;
   $$self{"MON"} = $mon + 1;
   $$self{"YEAR"} = $year + 1900;
   return bless $self;
sub getDate {
   my $this = shift;
   return $$this{"MON"}.'/'.$$this{"MDAY"}.'/'.$$this{"YEAR"};
sub getTime {
   my $this = shift;
   return $$this{"HOUR"}.":".$$this{"MIN"}.":".$$this{"SEC"};
sub setYear {
   my $this = shift;
   $$this{"YEAR"} = shift;
sub getAuthor {
   return $author;
sub getVersion {
   return $version;
sub getInfo {
   return $info;

Here is a program to test the CalendarClass class:

#- Copyright (c) 1999 by Dr. Herong Yang,
   use CalendarClass;
   $c = CalendarClass->new();
   print("\nTest 1:\n");
   print("   Date and time: ",$c->getDate()," ",$c->getTime(),"\n");
   print("   Date and time: ",$c->getDate()," ",$c->getTime(),"\n");
   print("\nTest 2:\n");
   print("   Year: ",$$c{"YEAR"},"\n");
   print("   Year: ",$c->{"YEAR"},"\n");
   print("\nTest 3:\n");
   print("   Author: ",CalendarClass->getAuthor(),"\n");
   print("   Version: ",CalendarClass::getVersion(),"\n");
   print("   Info: ",$c->getInfo(),"\n");


Test 1:
   Date and time: 12/20/1999 23:53:57
   Date and time: 12/20/2000 23:53:57

Test 2:
   Year: 2000
   Year: 2000

Test 3:
   Author: Dr. Herong Yang,
   Version: 1.00
   Info: My first Perl class.

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