I can't agree:

All attributes have global scope.

What does that mean? What global scope shall these attributes have?

Typing $self->{fo} when you meant to type $self->{foo} won't give you a compile time error.

You can't solve this problem as long as you use a hash at all. But the danger can be limited to inside the class by enforcing accessor methods. Your Code doesn't help this either.

You can't inherit from classes implemented with non-hash references.

You can. You cannot implement non-hash objects using the OO module, but you can inherit from any object, because _your_ class' data is kept somehwere else. Anyways: the new() was in question. It's probably no good idea, although you're always allowed to override it.

oo_get/set allows you to get the attribute from any class through your optional third argument.

Just as Perl gives you symbolic references. This is for example needed when you create accessor methods automatically.

The idea of putting the data back into the object seperated by classes is great. It makes the object classically serializable and gets us rid of complicated DESTROY methods because it's all in one place. "Ay, there's the rob", it is then impossible to inherit from classes that don't use that new scheme.

If you can accept it that you only need the new scheme for new classes and not for subclassing others, it's quite easy to put that abstraction into a superclass as well (also avoiding use of __PACKAGE__):

package OO; use strict; use warnings; # A constructor sub oo_create { my $class = shift; bless {}, $class; } # Set Data reference for current class sub oo_set { my $obj = shift; my $val = shift; my $class = @_ ? shift : caller; $obj->{ $class } = $val; } # Get Data reference for current class sub oo_get { my $obj = shift; my $class = @_ ? shift : caller; $obj->{ $class }; } 1;

In use that's:

package Foo; use base 'OO'; sub new { my $class = shift; ( my $self = $class->oo_create )->oo_set( [] ); # array based object! $self } sub bar { my $self = shift; my $data = $self->oo_get; $data->[0] = shift if @_; $data->[0] }

You could now even use OO as library, not as class...

I think we're on the right way ;)


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

and:  <code> code here </code>