> > > My next direction is OOP, but I have found it to be a
> > > little beyond my grasp each time I try. Maybe with this
> > > list's help I can get it.
> wow finaly an explanation of OOP that I can understand !!!!!
> what an elegant way of explaining it , thanks
Thanks for the thanks, Greg and Tim. That encourages me
to add a little more (now you got me started). The extra stuff
follows a repost of the original intro.
OOP in a few seconds
====================
First you have to unlearn.
Imagine you knew nothing about programming.
Now, I introduce a thing called a variable:
my Dog $spot;
(ok, the current syntax is different, but let's ignore that.)
Accept the natural English reading of this.
One would not be too surprised to see that followed by:
$spot->bark;
or
$spot->hair(long);
One would also not be too surprised to hear that you
don't have to write the recipe for bark and hair -- that
this is something known by all Dogs.
And that:
my Dog $rex;
has a hair length independent of $spot's.
Imo, that's pretty much all there is to it. Everything
else is just icing on the OOP cake (more functionality)
plus the concrete syntax (a little daunting at first).
Some icing follows...
spot and rex go walkies
=======================
You might take $spot and $rex to the park. Perhaps
dogs don't pee on trees that have been stripped of
their covering. So, your code might include:
my Tree $tree is stripped;
# (again, the syntax isn't like that, but never mind.)
foreach my $dog ($spot, $rex) {
if ($tree->bark) then {
$dog->pee;
}
}
}
What's going on here? Well, these:
$spot->bark;
$rex->bark;
result in calls of the same code. They might result in
some sound coming out of your computer's speaker.
But this:
$tree->bark;
calls completely different code. It is clearly supposed
to return a true/false result, presumably based on
whether the given Tree is stripped of bark or not.
Perl knows what bark code to pick based on the object --
$spot and $rex are Dogs, and $tree is a Tree.
Basically, a Dog variable has a different "namespace"
(the Dog class) in which to look up things than a Tree
(which looks for things in the Tree class).
Lassy
=====
Now for a simple Dog trick with big consequences.
What if you had:
my GoldenRetriever $lassy;
$lassy->bark;
In the above, you are using the GoldenRetriever class
that someone else wrote, just as you were with Dog
and Tree. What do you think ->bark does?
->bark could test whether $lassy had a coat, just like
->bark does when applied to a Tree. More likely, it
does something similar to what happens to $spot
when he ->bark's. It all depends on what the designer
did as s/he coded the GoldenRetriever class.
One very likely possibility is that, in the code for the
class, s/he had a line something like:
class GoldenRetriever isa Dog;
(ok, the syntax is different, but that's just detail).
This means that, unless the GoldenRetriever class
designer went on to redefine bark for that class, that
$lassy->bark will invoke the same code as $spot->bark.
Even if the GoldenRetriever class designer decided
to redo bark, s/he would be pretty much bound by
common sense to make it a dog sound like thing
like Dog has, rather than a coat covering check like
Tree has. So you can safely say:
$lassy->bark;
and pretty much expect it to do something sensible.
Thus GoldenRetrievers probably support the same
sort of ->this and ->that that Dogs do, but maybe
with GoldenRetriever specific wrinkles. Perhaps
someone created a:
class Retriever isa Dog;
and the GoldenRetriever class designer said:
class GoldenRetriever isa Retriever;
Then perhaps Retrievers (and thus GoldenRetrievers)
have a ->fetchball, whereas plain Dogs don't. As you
can see, it all gets rather arbitrary as to how you break
things up. THIS is the bit of OOP that can be both very
natural and frustratingly arbitrary at the same time.
A note on the syntax I used
===========================
The -> syntax is correct for Perl 5. All the other syntax
I've used is more or less how Perl 6 is going to do things.
For completeness, I'll mention that Perl 6 is going to use:
$spot.bark;
rather than
$spot->bark;
The principles are, regardless, the same.