Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

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

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:

  • Should bless be automatically redefined or explicitly as shown in the example above? It would almost always be imported, but making it explicit highlights the fact that bless is doing something unusual. Without the import, it could be called as InsideOut::Helper::bless( $self, $class ) of course.

  • Is there anything unusual about bless' syntax or function that I need to watch out for in redefining it? (Should I use a prototype that matches the builtin?)

  • Is redefining bless a bad idea? Should I just import a separate function like register instead to register the object after it's blessed using the built-in bless? E.g. register( bless $self, $class )

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



  • 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 scrutinizing the Monastery: (6)
As of 2024-04-19 12:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found