On the Subject of Objects
From JmPm
Contents |
Object orientation
Some basic oops concepts
blessed objects
inheritance
polymorphism
encapsulation
persistence
methods
Inheritance
@ISA
constructors
Diamonds-multiple inheritance
can()
isa()
UNIVERSAL
AUTOLOAD
SUPER
derived classes
empty subclass test
abstract methods
@ISA
Our @ISA qw(whatever);
Parents
Left-right, down-up
Use base (prefered) –which is:
BEGIN { require Foo; require Bar; push @ISA, qw(Foo Bar); }
But .. Cannot change @ISA , packages may not be in single unique .pm
Perhaps we do not need inheritance at all ?
constructors
2 argument bless
Generalized “new” can then call the blessed class to fill attributes
Sub new{ My ($class,%args)=@_; My $self =bless{}, ref($class)||$class; $self-> _init(%args); Return $self; }
Diamonds-
Sub _init{ My ($self,%args)=@_; Return if $self->{_init}{__PACKAGE__}++ Do other init things }
can()
Checks through all @ISA inheritance
Returns reference to method that can be called
isa()
Checks if the named method is an ancestor of the current method
UNIVERSAL
The base class from which all classes are derived-looks here after @ISA
Can put a general method in UNIVERSAL
isa() and can() are here
AUTOLOAD
Method AUTOLOAD can be anywhere in the hierarchy
If a method is not found and there is an AUTOLOAD, it is executed
$AUTOLOAD=the full method name of what was not found
For long seldom used subroutines
SUPER
$self->SUPER::some_method;
Looks for method in the parent of the current class – not the parent of the invoking object
Uses same search as usual in @ISA etc.
derived classes
Care must be taken not to mix up variable names of inherited data
Use prefix of class name
empty subclass test
Call a method from an empty child method and see if it is found
abstract methods
Placeholder in the parent class that throws and exception, insuring that all of the children have that same named method
references
Object Oriented Perl: A Comprehensive Guide to Concepts and Programming Techniques by Damian Conway
Learning Perl Objects, References, and Modules by Randal L. Schwartz and Tom Phoenix
Advanced Perl Programming by Simon Cozens
Programming Perl (3rd Edition) by Larry Wall, Tom Christiansen, and Jon Orwant
Perl Best Practices by Damian Conway