I'm looking for the right OO approach to what I think amounts to perl 6 roles.
I have an Object base class which is inherited by other objects eg User, Group, Image. When each of these is loaded, they register their Type with the Object class so that any object can be retrieved using the parent class and blessed into the right package.
sub find {
my $object = __PACKAGE__->retrieve_object($id);
bless $object,$object->type->class
An Object can never exist on its own - it must always be blessed into a child class.
Group is a child class, and any object (including Groups) can be a member of a Group. Also, every object has a default Group (eg User automatically belongs to the group all_users).
Requirements
-
It makes sense to store these default Groups as singletons in a hash so that they don't have to be recreated for every object.
-
I would also like a method groups() in each child class which asks the Group class for a list of relevant groups.
So, as I see it,
I need to: - register each child class with the Group class (so that it knows what the default group is, and
- have some form of code reuse so the the groups() method doesn't need to be included in each child class.
I thought of doing something like this:
use Group 'default_group'
Then in the package Group:
sub import {
my $default_group = $_[1];
my $callpkg = caller(0);
register_default_group($callpkg,$default_group);
no strict 'refs';
*{$callpkg."::groups"} = \&groups;
use strict;
Is this good, bad, ugly? Beauty, truth and love appreciated
thanks
clint
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.