I've got the following situation:
- I have a number of classes that all inherit from some base class. Let's call them Foo and Bar inheriting from Base.
- Now, I have a bunch of class information, such as Required attributes, that every class inheriting from Base will be setting up. The function to see if a given attribute is required or not is the same for all children, hence I want to put it in the Base class.
- It used to be that this class information was stored in every instance of this class. As there's a ton of this information and I might be instantiating 1000+ objects of a given class, it makes sense to put this at the package level.
The problem I'm having is that the function in Base can't access the information in the package of Foo or Bar.
So, I'm thinking about this and I came up with a number of possible solutions.
- Have the function in Base use soft references. Ugh!
sub isRequired {
my $self = shift;
my $attr = shift;
my $class = ref($self) or croak "No class name!\n";
no strict 'refs';
return (${"$class::Required"}{$attr});
}
- Have each object have an attribute called 'Required' and have that contain a reference to the package-scoped variable. This cuts down on the duplicated information, but it still feels like I'm having to kludge. The function would now look like
sub isRequired {
my $self = shift;
my $attr = shift;
return ($self->{Required}{$attr});
}
I guess what I'm bitching about is that even though $self is from package Foo or Bar, blah() is executing within the namespace of Base. Is there some way I can get around this? Is there another solution I'm not seeing?
------
We are the carpenters and bricklayers of the Information Age.
Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.