Let's look at the main problems with hash based objects that Abigail-II's inside-out objects solve:

  1. A sub-class can accidentally overwrite a super-class's attribute, by using the same hash key, without causing a compile time error.
  2. All attributes have global scope.
  3. Typing $self->{fo} when you meant to type $self->{foo} won't give you a compile time error.
  4. You can't inherit from classes implemented with non-hash references.

Unfortunately, you're class only solves the first one :-)

You could get much the same affect as your class by the traditional hash based approach, but adding another layer of indirection based on the class, e.g.:

{ package Foo; sub new { bless {}, shift; }; sub foo1 { my $self = shift; @_ ? $self->{+__PACKAGE__}->{foo} = shift : $self->{+__PACKAGE__}->{foo} }; }; { package FooBar; use base qw(Foo); sub foo2 { my $self = shift; @_ ? $self->{+__PACKAGE__}->{foo} = shift : $self->{+__PACKAGE__}->{foo} }; }; my $o = FooBar->new; $o->foo1("the foo from the base class"); $o->foo2("the foo from the sub-class"); print $o->foo1, "\n"; print $o->foo2, "\n"; # produces the foo from the base class the foo from the sub-class

... but you don't have to worry about the DESTROY method :-)

You also have the problems with overloading and re-blessing objects, but that's solvable like this.

It might be worth having another read of the first half of this node where Abigail-II talks, somewhat forcefully, about the strict/scoping issues :-)

I've got a base class that does some of the stuff that I think you're after... I'll spend a little time and clean it up enough to make public :-)


In reply to Re: A different OO approach by adrianh
in thread A different OO approach by fruiture

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.