Hi all :-),

I recently decided to convert one of my more complex library classes over to the outside-in class model. I'm using Perl 5.8 and rather than use say Class::Std (does that even work with early 5.8?) and add another dependency to my library for the sake of 10 lines of code, I have implemented it directly. I used XDG's posting Threads and fork and CLONE, oh my! as the basis.

Anyway, it's all working as it should :-). However one thing I did differently was to cache the address inside the blessed object rather than `recompute/fetch' it with refaddr() each time I needed it. So instead of doing something like:

my %class_records; my $class_objects; . . sub new($) { my $class = (ref($_[0]) ne "") ? ref($_[0]) : $_[0]; my $this = {attr1 => "Hello World"}; my $self = bless({}, $class); my $id = refaddr($self); $class_records{$id} = $this; $class_objects{$id} = $self; weaken($class_objects{$id}); return $self; } sub some_method($) { my $self = $_[0]; my $this = $class_records{$refaddr($self)}; ... }
I did:
my $class_name = __PACKAGE__; my %class_records; my $class_objects; . . sub new($) { my $class = (ref($_[0]) ne "") ? ref($_[0]) : $_[0]; my $this = {attr1 => "Hello World"}; my $self = bless({}, $class); my $id = refaddr($self); $self->{$class_name} = $id; $class_records{$id} = $this; $class_objects{$id} = $self; weaken($class_objects{$id}); return $self; } sub some_method($) { my $self = $_[0]; my $this = $class_records{$self->{$class_name}}; ... }
Note: The %class_objects hash is used to keep track of objects and refiling them during cloning. It is equivalent to XDG's %REGISTRY hash.

Since I never use refaddr() again on an object once it is created do I need to bother with the refiling that goes on in his CLONE() method when it comes to threading (ithread)? I think not... My reasoning is this:

This issue can easily be overcome with a simple clash detector and then there is no need for CLONE() nor %class_objects. After all refaddr is being used to generate unique ids cheaply.

Is my thinking sound or have I got inadvertently eaten some dodgy mushrooms! :-)

Anyway, many thanks in advance.

Tony.


In reply to Are Addresses From refaddr Unique Across Threads? by aecooper

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.