Perhaps the problem is somewhere else in your code. Filling in the blanks a little, it seems to work for me.
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.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 -->
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.
In reply to Re: Generic accessor for inside-out objects...
by kyle
in thread Generic accessor for inside-out objects...
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |