According to most online sources OO programming involves three basic elements: inheritance, polymorphism, and data encapsulation (see references included in the code). Some examples of basic ways to achieve these are below.
#... http://www.cs.mun.ca/~donald/bsc/node12.html
# INHERITANCE
package dog;
sub new {
my $class = shift;
return(bless({'puppies'=>15},$class));
}
sub get {
my ($self,$name) = @_;
return ($self->{$name});
}
sub set {
my ($self, $name, $newvalue) = @_;
#this is silly but saves space
$self->{$name} = $newvalue;
return $self->{$name};
}
sub bark {
return 'woof...I INHERITED this capability.';
}
package cockerSpaniel;
use parent -norequire, qw|dog|;
sub new {
my ($class) = @_;
return(bless(dog->new(),$class));
}
sub bark {
my ($class) = @_;
return ($class->SUPER::bark() . '..but then overrode it');
}
package main;
my $doggie = new cockerSpaniel();
$doggie->set('puppies',29);
print qq|\nHere are the cockerSpaniel's puppies: | . $doggie->get('pup
+pies');
my $realdog = new dog();
print qq|\nHere are the parent dog's puppies: | . $realdog->get('puppi
+es');
# POLYMORPHISM, or dynamic binding of function calls
print qq|\n| . $doggie->bark();
# Data Encapsulation
# There are numerous techniques. Take your pick:
# http://www.csse.monash.edu.au/~damian/TPC/1999/Encapsulation/Paper.h
+tml
The above, with the reading, should take only about an hour or so to learn. It is good, in my opinion, to learn the old non-Moose way of defining classes since there is so much code out there that uses these techniques.
The problem that I have seen with OO programming in general has little to do with the techniques or semantics -- it has to do with learning to
think in an object oriented way. For this I recommend using something entirely outside of the scope of a particular language. I recommend reading
Object Thinking by David West (don't worry that is was published by Microsoft Press...this book was published under an experiment where no Microsoft technologies were discussed in the book).
Here you will learn not just terminology and the mechanics of how to migrate from top-down to OO, but how to use nomenclatures and tools (CRC cards, for instance) that will help you describe and analyze problems before you write code.
Celebrate Intellectual Diversity
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.