<?xml version="1.0" encoding="windows-1252"?>
<node id="219131" title="Re: Tutorial: Introduction to Object-Oriented Programming" created="2002-12-11 12:23:00" updated="2005-08-15 10:51:13">
<type id="11">
note</type>
<author id="169744">
Abigail-II</author>
<data>
<field name="doctext">
Too bad you are teaching people bad habits. I know using a hash reference
to store you attributes in is common, but it's bad habit. Here on
perlmonks, people scramble over each other if someone dares to post code
that doesn't use 'strict' to scold at the person for not using strict.
&lt;p&gt;
Using hashes to store object attributes in is like writing non-OO code
using global variables, no package names and certainly no strict.
You're loudly declaring that you don't want the benefits of stricts,
namespaces or scoping. On top of that, you're tossing one of the pillars
of OO programming out of the window: encapsulation. You're not taking
any steps to prevent hiding your implementation - it's even worse,
you are &lt;em&gt;forcing&lt;/em&gt; the classes that inherits you to adapt your
implementation.
&lt;p&gt;
Let's look at some issues. Suppose we have an class, &lt;code&gt;Animal&lt;/code&gt;
whose instances have an attribute, &lt;code&gt;species&lt;/code&gt;. We write 
two accessor methods, &lt;code&gt;get_species&lt;/code&gt; and &lt;code&gt;set_species&lt;/code&gt;:
&lt;code&gt;
    sub set_species {
        my ($self, $species) = @_;
        $self -&gt; {speceis} = $species;
    }
    sub get_species {
        my ($self) = @_;
        $self -&gt; {species};
    }  
&lt;/code&gt;
&lt;p&gt;
This code compiles fine. No matter how many warnings and strictness
you have turned on. It will even run fine, at best you get somewhere
down the line a warning that you are using an undefined value. The
equivalent non-OO code wouldn't even compile with strictness turned on.
&lt;code&gt;
    my $species;
    sub set_species {
        $speceis = shift;
    }
    sub get_species {
        $species;
    }
&lt;/code&gt;
&lt;p&gt;
Same typo, but a compile time error.
&lt;p&gt;
And here's how you are not using proper encapsulation. Suppose you
have a generic &lt;code&gt;Document&lt;/code&gt; class. The generic class
gives you some minimal functionality, like adding and deleting text.
Suppose that the class wants to track the number of lines in the  
document. It's an attribute that's only used in this class, so
it's even using the "convention" to put an underscore in front of
the attribute name. The number of lines is stored in the attribute
&lt;code&gt;_lines&lt;/code&gt;. Now we subclass the generic class to create
a &lt;code&gt;LaTeX&lt;/code&gt; class, for LaTeX documents. As part of the
functionality, we also keep track of the amount of lines of the
generated &lt;code&gt;dvi&lt;/code&gt; output. Since this will be a private
attribute as well, we use &lt;code&gt;_lines&lt;/code&gt; as attribute.
&lt;p&gt;
&lt;strong&gt;DONG DONG DONG!&lt;/strong&gt; That will give problems.
Sneaky problems, that won't
be detected at compile time - and not even give warnings at run time,
no matter how many warnings you turn on.  Perl gives you name spaces,
and scopes, but you aren't using them, not even in an OO program where
encapsulation is one of the key elements.
&lt;p&gt;
Note that you have a similar problem in your code when you are using the
underscore convention. You call &lt;code&gt;_open&lt;/code&gt; as a &lt;em&gt;method&lt;/em&gt;.
Which means that if there's an inheriting class that uses the same
convention, you call the subroutine in the class, not your own. Better
call it sub as a sub, not as a method. Why would you? It's intended to
be a private method, so you don't want to call an inherited method anyway.
&lt;p&gt;
The bottom line is, if you want to use object oriented programming,
you're better off using a language designed by someone who understood
the concepts of object orientness. And not a language that found a
cute trick to use arrows.
&lt;p&gt;
Abigail
</field>
<field name="root_node">
218778</field>
<field name="parent_node">
218778</field>
</data>
</node>
