in reply to converting to a sub-class

Best practices, I don't know, but your construct is limited in that it will only work if the DUM::DUMMY::Whatever objects don't need any initialization (you're just blessing directly into that class).

What you're doing will work, though it may confuse people. The big question is, do you really need a DUM::DUMMY object at all, when you're going to move it to DUM::DUMMY::Whatever anyway?

If you can, I would prefer doing something like:

sub create { my ($class,@options) = @_; my $target_class = $class->find_class_for(@options); return $target_class->new(@options); }
and have find_class_for return the right package name (and possibly require it). That way the object returned can do whatever it needs to initialize itself - it doesn't even need to be a subclass of DUM::DUMMY.

Another thing to keep in mind, is that this still requires you to put all the knowledge about which class to choose in the base class. That isn't really "best practice", and it's possible to avoid it, but unless you're got many classes to choose from, it's probably the simplest way to do it like this.

Replies are listed 'Best First'.
Re^2: converting to a sub-class
by Cagao (Monk) on Oct 20, 2007 at 18:07 UTC
    I see where you're coming from with that, but I'm trying to take a class, then simply to specialise it in whatever way, ie, modified methods, or even new methods (although I doubt I'll be doing that).

    I seem to be having a problem with this approach due to Class::Data::Inheritable and Class::Accessor

    Those are both used in the base class for my 'initial' class to setup the constructor, etc.

    those behaviours from the base class are present in the main class, but not in sub-classes when using the methods I've shown in my first example.