<?xml version="1.0" encoding="windows-1252"?>
<node id="219441" title="Re^3: Tutorial: Introduction to Object-Oriented Programming" created="2002-12-12 17:02:53" updated="2005-07-27 14:04:23">
<type id="11">
note</type>
<author id="186362">
adrianh</author>
<data>
<field name="doctext">
&lt;p&gt;[Abigail-II] will be better able to comment than myself, but I've been playing around with her
 technique on-and-off since I [id://178518|discovered it on perlmonks]. While I've not been doing it in anger, it's not been entirely in jest either. I've been re-writing some [cpan://Test::Class] hierarchies that were fairly deep, and hence possible sources for hash key clashes.&lt;/p&gt;

&lt;p&gt;In general I've not had problems. It's a &lt;em&gt;very&lt;/em&gt; nice hack to get around perl's lack of object attribute encapsulation.  I'll probably use the technique in the next production project I do.&lt;/p&gt;

&lt;p&gt;[Abigail-II] - have you considered writing a little tutorial on the subject? :-)&lt;/p&gt;

&lt;p&gt;Issues that can cause problems:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;
        &lt;p&gt;You have to take overloading the "" operator into  account. Since the attribute objects are indexed by "$self", you have to use &lt;code&gt;overload::StrVal&lt;/code&gt; something like this:&lt;/p&gt;
        &lt;code&gt;
            package Foo;
            use strict;
            use warnings;
            use overload;
            
            my %foo = ();
            
            sub new {
                my $class = shift;
                bless [], $class;
            };
            
            sub foo {
                my $self = overload::StrVal shift;
                $foo{$self} = @_ if @_;
                $foo{$self};
            };
            
            sub DESTROY {
                my $self = overload::StrVal shift;
                delete $foo{$self};
            };
        &lt;/code&gt;
        &lt;p&gt;if there is any chance that somebody will write a sub-class that overrides "", for example:&lt;/p&gt;
        &lt;code&gt;
            package Foo::Pretty;
            use base qw(Foo);
            use overload
                q{""}   =&gt;  sub {
                    my $self = shift;
                    my $foo = defined($self-&gt;foo) ? $self-&gt;foo : 'undef';
                    "&lt;foo=$foo&gt;";
                },
            ;
        &lt;/code&gt;
    &lt;/li&gt;
    &lt;li&gt;
        &lt;p&gt;Having to remember to add/remove attribute hashes from the &lt;code&gt;DESTROY&lt;/code&gt; method can be a pain. I tend to move attributes around classes a fair bit during refactoring. Forget to add an appropriate &lt;code&gt;delete&lt;/code&gt; line to the &lt;code&gt;DESTROY&lt;/code&gt; method and you suddenly have a nasty little memory leak. I've had this happen to me more often in the few weeks I've been playing  with this technique than I've had hash/method name clashes in the several years I've been writing OO perl - but that may be lack of practice :-)&lt;/p&gt;
    &lt;/li&gt;
    &lt;li&gt;
        &lt;p&gt;Object serialisation and other reflective acts become harder. No more quick'n'dirty throwing your object at [cpan://Storable] to serialise your object. No more throwing your object at [cpan://Data::Dumper] to get a snapshot of its state. This could be considered a feature depending on your point of view.&lt;/p&gt;
    &lt;/li&gt;
    &lt;li&gt;
        &lt;p&gt;I've not had the chance to try teaching this method to perl/OO novices. My hunch is that it will turn out harder than hashes. Perl novices are already used to stuffing stuff in hashes, so it's a natural progression. OO novices latch onto the idea that an object is a "thing". My guess is that a single "thing" spread over several hashes goes against the naive concept of what an object is, and will therefore be harder to teach.&lt;p&gt;
    &lt;/li&gt;
    &lt;li&gt;&lt;p&gt;
        Having to list all of the attributes in the &lt;code&gt;DESTROY&lt;/code&gt; method, as well as declare them at the top offends my sense of [http://c2.com/cgi/wiki?OnceAndOnlyOnce|once and only once]. 
    &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
</field>
<field name="root_node">
218778</field>
<field name="parent_node">
219397</field>
</data>
</node>
