Re: hash of objects inside an instance

[email protected] (Lars Balker Rasmussen)
Newsgroups perl.moose
Message-ID <CAB-_BaRggtJAPn0paM90v6O-EbpwtT6SgKvjd30-=g+XCUyp5Q@mail.gmail.com>
On Mon, Nov 28, 2011 at 5:08 PM, Erik Colson <[email protected]> wrote:

> 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.
>

No, what you want is an abstraction.

Basically you're trying to hack in something that's actually
business-information connecting several objects.

In relational databases, you know it'd be wrong to do something like this
in the employee table; the principle is much the same here.

Maybe add an extra class WorksFor[1] containing the cost and department,
and have a list of WorksFor in employee. (Warning, very little thought went
into this and all my brain-blood is occupied around full stomach, but it
solves the basic problem :) )

PS: When you're exposing hash/array-references to users of your class,
you're begging for problems down the road - always[2] encapsulate
implementation.

[1] I suck at naming
[2] Always doesn't always mean always.
-- 
Lars Balker Rasmussen                                        Consult::Perl
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.