|
perlobj - Perl Objects
Part:
1
2
3
4
5
(Continued from previous part...)
#- AccountClassTest.pl
#- 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");
$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;
The output is the same as ObjectVariableTest.pl, 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 behide the objects.
A Perl Class Example - CalendarClass.pm
Here is another class that shows you how to define class methods, object
methods and object creation method, CalendarClass.pm:
#- CalendarClass.pm
#- 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;
}
1;
Here is a program to test the CalendarClass class:
#- CalendarClassTest.pl
#- 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");
$c->setYear("2000");
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");
exit;
Output:
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.
Conclusions:
- A class is package with a new() subroutine to return an object, and
other subroutines to be invoked as class methods and object methods.
- An object is reference blessed with a package name.
- Class methods and object methods are invoked with the "->" notation.
- Of course, other object-oriented concepts are also simulated by Perl.
They will be discussed in other chapters.
Part:
1
2
3
4
5
|