Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Comment on

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

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

  • oo_get/set allows you to get the attribute from any class through your optional third argument.
  • Since attributes are still hash keys you can mis-spell an attribute name without any compile time errors.
  • Having your own new() method makes mixing this into an existing class hard.

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (11)
    As of 2014-10-02 15:18 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      What is your favourite meta-syntactic variable name?














      Results (64 votes), past polls