#!/usr/local/apps/perl/bin/perl ############################################################################# # # # WHO: John L. Moreland # # # # WHAT: oop # # # # WHY: Demonstrates the use of Object Oriented Programming in Perl. # # # # WHERE: Opus Software # # # # WHEN: Wed Sep 30 19:18:11 PDT 1998 # # # # HOW: PERL # # # ############################################################################# ############################################################################# ################################# MYCLASS ################################# ############################################################################# package MyClass; sub new { print " MyClass::new called\n"; my $type = shift; # The package/type name my $self = {}; # Reference to empty hash return bless $self, $type; # Add the object to this class and return it } sub DESTROY { print " MyClass::DESTROY called\n"; } sub MyMethod { print " MyClass::MyMethod called!\n"; } ############################################################################# ############################### MYSUBCLASS ################################ ############################################################################# package MySubClass; @ISA = qw( MyClass ); sub new { print " MySubClass::new called\n"; my $type = shift; # The package/type name my $self = MyClass->new; # Reference to empty hash return bless $self, $type; # Add the object to this class and return it } sub DESTROY { print " MySubClass::DESTROY called\n"; } sub MyMethod { my $self = shift; $self->SUPER::MyMethod(); print " MySubClass::MyMethod called!\n"; } ############################################################################# ################################## MAIN ################################### ############################################################################# package main; print "Invoke MyClass method\n"; $myObject = MyClass->new(); $myObject->MyMethod(); print "Invoke MySubClass method\n"; $myObject2 = MySubClass->new(); $myObject2->MyMethod(); print "Create a scoped object\n"; { my $myObject2 = MyClass->new(); } # Destructor is called automatically here print "Create and undef an object\n"; $myObject3 = MyClass->new(); undef $myObject3; print "Fall off the end of the script...\n"; # Remaining destructors are called automatically here