On the Subject of Objects
From JmPm
(New page: == Object orientation == Some basic oops concepts blessed objects inheritance polymorphism encapsulation persistence methods == Inheritance == @IS...) |
|||
(3 intermediate revisions not shown) | |||
Line 1: | Line 1: | ||
+ | [[Image:Slide2.JPG]] | ||
+ | |||
== Object orientation == | == Object orientation == | ||
Line 43: | Line 45: | ||
== @ISA == | == @ISA == | ||
- | constructors | + | 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 | 2 argument bless | ||
Generalized “new” can then call the blessed class to fill attributes | 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- == | == Diamonds- == | ||
+ | test for multiple inheritance | ||
Sub _init{ | Sub _init{ | ||
Line 57: | Line 85: | ||
Do other init things | Do other init things | ||
} | } | ||
+ | |||
+ | This will work also for destructors or any other module that you do not want to do more than once | ||
+ | |||
+ | Loop through all of the parents | ||
+ | |||
+ | Sub somesub{ | ||
+ | $self=shift; | ||
+ | Foreach my $parent(@ISA){ | ||
+ | my $ancestor_sub=$parent->can(“somesub”); | ||
+ | $self-> $ancestor_sub() if $ancestor_sub; | ||
+ | …code…. | ||
+ | } | ||
+ | |||
+ | $self->$_for (map{$_->can(“somesub”)||()} @ISA); | ||
+ | |||
== can() == | == can() == | ||
Line 64: | Line 107: | ||
Returns reference to method that can be called | Returns reference to method that can be called | ||
+ | My $method_call=$obj->can(“do1”) | ||
+ | || =$obj->can(“do2”) || =$obj->can(“do3”) ; | ||
+ | Return $obj ->$method_call() if $method_call; | ||
Line 78: | Line 124: | ||
isa() and can() are here | isa() and can() are here | ||
+ | |||
+ | Sub UNIVERSAL::_debug{ | ||
+ | ..do debug here… | ||
+ | } | ||
Current revision as of 12:51, 7 December 2008
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-
test for multiple inheritance
Sub _init{ My ($self,%args)=@_; Return if $self->{_init}{__PACKAGE__}++ Do other init things }
This will work also for destructors or any other module that you do not want to do more than once
Loop through all of the parents
Sub somesub{ $self=shift; Foreach my $parent(@ISA){ my $ancestor_sub=$parent->can(“somesub”); $self-> $ancestor_sub() if $ancestor_sub; …code…. }
$self->$_for (map{$_->can(“somesub”)||()} @ISA);
can()
Checks through all @ISA inheritance
Returns reference to method that can be called
My $method_call=$obj->can(“do1”) || =$obj->can(“do2”) || =$obj->can(“do3”) ; Return $obj ->$method_call() if $method_call;
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
Sub UNIVERSAL::_debug{ ..do debug 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