Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

What are you trying to achieve with this approach? There are many ways to make "inside-out" objects. The core idea is just that the object (the blessed reference) either (a) contains or (b) is itself (via it's memory address) an index to some other data storage container. That storage container could be almost anything, and accessed in almost any way.

One reason that inside-out objects are interesting people lately is that it's possible, even easy, to make the container a "private" lexical and have the accessor reach it via a closure. On the other hand, you can make the storage global and do some really wild aliasing, too. (See my Object::LocalVars if you want to bend your brain.) It all depends what you're trying to accomplish.

I'm not sure what the array storage gets you over a hash -- maybe a bit of speed, but extra hassle dealing with reclaiming memory as other posters have noted. One downside to the array is that it's possible to manipulate the index inside the blessed reference and potentially peek into other objects. I think Conway talks about this in Object Oriented Perl.

With a hash and "unique" indices, you avoid that problem. On the other hand, as I pointed out in 'Threads and fork and CLONE, oh my!', using a memory address as a unique index breaks under threads or a pseudo-fork on Win32 unless you use Perl 5.8 and go to some pains with CLONE and again sacrifice efficiency for book-keeping.

I'm not convinced there's a "right" way to do inside-out objects. Until Perl 6 anyway. (/me grins)

I'm currently experimenting in a couple ways. With lots of attributes, keeping each in a separate hash like how Class::Std does is costly for destruction. I've been speculating whether the right approach is to keep one hash keyed on the index (needed for CLONE anyway), and have the contents be a hash reference with the keys being the individual attributes. Then DESTROY only needs to clean up one reference in the master hash, and let Perl's garbage collection take care of the rest.

On the more bent side, I'm considering that approach but using anonymous symbol table refs, so I can get that elegance in cleanup and still use the local aliasing trick in Object::LocalVars. (Why do I feel like I'm on the path to the Dark Side when I say that...)

So... if you want to use arrays for your inside-out objects, you can -- but know why you're doing it and what that gains you and what you give up in doing so.

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.


In reply to Re: inside-out objects using arrays? by xdg
in thread inside-out objects using arrays? by rvosa

Title:
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 How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-03-29 00:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found