in reply to Generic accessor for inside-out objects...

Perhaps the problem is somewhere else in your code. Filling in the blanks a little, it seems to work for me.

package Foo; use Scalar::Util qw( refaddr ); use Carp; my %a_of; my %b_of; sub new { my $self = \do{my $x}; bless $self, __PACKAGE__; $a_of{ refaddr $self } = 'a: ' . rand; $b_of{ refaddr $self } = 'b: ' . rand; return $self; } sub puke { my $self = shift; print $a_of{ refaddr $self }, "\n"; print $b_of{ refaddr $self }, "\n"; return; } sub get { # pasted from OP } package main; my $o = Foo->new(); $o->puke(); print 'a --> ', $o->get('a'), "\n"; print 'b --> ', $o->get('b'), "\n"; print 'c --> ', $o->get('c'), "\n"; __END__ a: 0.917827691956521 b: 0.0455599141746177 a --> a: 0.917827691956521 b --> b: 0.0455599141746177 Use of uninitialized value in print at /home/kyle/perlmonks.pl line 58 +. c -->
If you're trying to write one get method in some other package and import it into each inside-out class, that won't work for the reasons Joost has already mentioned.

Instead, maybe you should use the access methods available on the object. I did something like that in Make an inside-out object look hash-based using overload..

sub FETCH { my ($self, $key) = @_; my $getter = "get_$key"; return $self->$getter(); }

This way, you don't expose any private attributes, and you go through the proper access method, which might have some code in it more complicated that returning the attribute raw.

Replies are listed 'Best First'.
Re^2: Generic accessor for inside-out objects...
by EvanK (Chaplain) on Dec 11, 2007 at 15:46 UTC
    I wonder if the original poster is running on a mac? I'm running osx, and when running the given example script, i get:
    a: 0.233512969994084 b: 0.77414042744519 a --> b --> c -->
    just a thought, perhaps that helps.

    Update: Ah, sorry. Temporary case of PEBKAC :o

    __________
    Systems development is like banging your head against a wall...
    It's usually very painful, but if you're persistent, you'll get through it.

      The code I put in my node won't run verbatim. I was trying to save space when I wrote:

      sub get { # pasted from OP }

      Indeed, if I run it as it is, it gives the results you show. However, even on the Mac I tried (OS X 10.4.11), it works when I take the code for get from the OP.