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

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I would like to check that I have understood your technique properly. Do I have this correct?

The purpose of this module is to:

  • Allow arbitrary subclassing of any object without interfering with its super or subclasses
  • Only allow access to properties via methods, instead of accessing them directly via (eg) hash keys
  • Automate garbage collection

You do this by:

  • Accepting any type of object to new()
  • Taking the 'identity' of that object and, within MyClass (and within any other Alter Ego style class), associating that identity with (in your example) an anonymous hash, which will be used to contain the actual data relevant to MyClass
  • That anonymous hash is added to a bigger anonymous hash (the corona) which contains all the (Alter Ego) properties for this object: the key is the classname of the caller, the value is the created anon hash with the values relevant to that class:
    $corona = { # Alter ego for properties from ClassA ClassA => { foo => 'foo', bar => 'bar' }, # Alter ego for properties from ClassB ClassB => { baz => 'baz', foo => 'foo' # Doesn't interfere with t +he foo property from ClassA } }
  • To get/set a property that belongs to this class, you HAVE TO call a method which finds the alter ego (the associated anonymous hash) for the class in which the method lives (as determined by caller), and then that hash is used by the method to do the required work.

The obvious benefits are:

  • The properties belonging to a particular class cannot interfere with the properties from a super/sub-class
  • It doesn't matter what type the original object is - any object can be subclassed
  • It is fast and not convoluted
  • It catches run-time typos when trying to access properties (except in the methods which live in the class itself)
  • Questions

    • Tye's concern is that caller does not always return what we expect, because, although we may be calling package1::foo, that method could be aliased to package2::bar. As far as I can see, that should not matter, because the value associated with foo (handled by bar) is still encapsulated, and so cannot be interfered with. There are other problems with this (see Using SUPER in dynamically generated subs), but I don't see the issue with this particular question. What am I missing?
    • I don't understand why garbage collection works? Why, when $object goes out of scope will the corona go out of scope too? As far as I can see, you're not storing a reference to the corona in the $object.
    • You mention of problem of serialization. Your solution doesn't help with this - you still need to provide Storable hooks for that, no?
    I hope I've understood this correctly - and corrections appreciated

    thanks,

    clint


    In reply to Re: Beyond Inside-Out by clinton
    in thread Beyond Inside-Out by Anno

    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, 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
  • 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (4)
    As of 2018-07-21 14:07 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















      Results (449 votes). Check out past polls.

      Notices?