aecooper has asked for the wisdom of the Perl Monks concerning the following question:
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:
I did: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)}; ... }
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.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}}; ... }
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:
Is my thinking sound or have I got inadvertently eaten some dodgy mushrooms! :-)
Anyway, many thanks in advance.
Tony.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Are Addresses From refaddr Unique Across Threads?
by Xiong (Hermit) on Feb 15, 2010 at 08:17 UTC | |
|
Re: Are Addresses From refaddr Unique Across Threads?
by JavaFan (Canon) on Feb 15, 2010 at 18:58 UTC | |
by ikegami (Patriarch) on Feb 19, 2010 at 21:26 UTC | |
| |
by BrowserUk (Patriarch) on Feb 15, 2010 at 19:45 UTC | |
|
Re: Are Addresses From refaddr Unique Across Threads?
by shmem (Chancellor) on Feb 15, 2010 at 17:34 UTC | |
|
Re: Are Addresses From refaddr Unique Across Threads?
by aecooper (Acolyte) on Feb 19, 2010 at 20:08 UTC | |
by tirwhan (Abbot) on Feb 20, 2010 at 10:48 UTC |