I've been experimenting with some approaches for making inside-out objects a little more user friendly. (c.f. Anti-inside-out-object-ism for some of the common complaints). One of the more obscure issues has to do with thread-safety. I presented an approach for that in Threads and fork and CLONE, oh my!. I'm considering ways of making that more automatic and transparent.

One approach I've considered is a helper module that would override/redefine bless to register objects transparently for use in CLONE (which would also be imported automatically). Usage would be just as with a normal constructor:

package My::Class; use InsideOut::Helper qw( bless ); sub new { my $class = shift; my $self = \do { my $scalar }; bless $self, $class; }

Behind the scenes, this bless would do something like this:

my $REGISTRY; sub bless { &CORE::bless; $REGISTRY{ $_[1] }{ refaddr $_[0] } = $_[0]; return $_[0]; }

$REGISTRY would also be used in CLONE to fix up inside-out objects during thread creation.

I'm a little cautious redefining something as critical as bless so I'd like feedback on this idea. Some specific questions I have include:

Side notes -- the example above is incomplete as inside-out lexical properties also need to be registered somehow for use with CLONE. (I'm working on a good, simple syntax for that.) The InsideOut::Helper name is also just a placeholder as I haven't come up with a name for this module, yet. (That said, what do people think about a new InsideOut top-level name to make a sharper distinction from the more traditional Class? I'm not sure it's needed, but I could see a case for it.)

Also, at least some inside-out generators automatically import a function like id as an alias to refaddr for use like this:

my $NAME_OF{ id $self } = "Larry"; # instead of my $NAME_OF{ refaddr $self } = "Larry";

Do people feel this kind of aliasing is helpful or symbol pollution?

Your feedback is greatly appreciated.

-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 Overriding bless for inside-out object safety by xdg

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, details, 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, summary, 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.