Hello Erik, I think you have some fundamental misunderstandings that I'd like to clear up before you go any further. I will assume that you haven't pasted code directly since it is very likely that your 'isa' doesn't work.
In Perl, hashes are simply associative arrays or dictionaries. The keys are always strings or forcibly stringified things. The data associated with that key is then stored and can be retrieved by that key. It sounds like you are using Department instances as keys which will simply not do what you think it will. You need to store the instances under a unique identifier of some kind, perhaps even using the ID or Name of the Department. As an example in somewhat pseudo-code: my @departments = $self->get_all_departments(); my $employee = $self->get_current_employee(); foreach my $department (@departments) { # If you enabled the Hash trait and setup the proper handles, this # will work # $employee->add_department($department->Id, $department); # Otherwise: $employee->departments->{$department->Id} = $department; } With all of that said, I would probably take a step back and make sure that you understand basic Perl data structures before venturing further into the woods. There are many excellent help resources in the form of books, material on the web (from selected sites) and IRC. Please start your adventure by looking at http://learn.perl.org and joining #perl-help on irc.perl.org. On Mon, 28 Nov 2011 17:08:41 +0100 Erik Colson <e...@ecocode.net> wrote: > Hi, > > I have two classes : Employee and Department > every Employee works for at least one department. Sometimes the cost > is distributed over 2 or more departments. So I'd want a hashref > inside the Employee in which the keys are Department instances and > the value a percentage to be attributed to that Department. > > I tried as follow: > > package Employee; > > has 'departments' => (is=>'ro', isa=>'hashref[Department]', > default=>{{}}); > > (I used traits also, but that doesn't really change the problem...) > > so know I can use: > > $an_employee -> departments -> {$a_department} = 100; > > and also use the same for retrieving the value, which works. > > The content of the $an_employee -> departments is a hash containing > keys like: 'Department=HASH(0x7fa823c87b48)' > > What bothers me then is that I can't use this string as a Department > object and i.e. send a message to a key object of the departments > hashref of an Employee. > > How can I achieve this ? > -- > erik -- Nicholas Perez XMPP/Email: n...@nickandperla.net http://search.cpan.org/~nperez/ http://github.com/nperez