Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2022-08-19 12:03 GMT
Find Nodes?
    Voting Booth?

    No recent polls found