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":