On Dec 06, Michel Rodriguez wrote:
> Hi,
>
> This has happened to me enough t??me that I think a module might be in
> order:
>
> I want to subclass an existing class,but I can't: the objects in this
> class are created by an other existing class. I don't control the way that
> class creates these objects.
>
> Typically, when using an XML parser that creates a tree, I use a module. I
> create an object ( "parser") in that module, which I can sub-class. But
> then, during the parsing, the parser in turn creates the nodes in the
> tree. I want to subclass those nodes, typically to add methods, and
> sometimes data, to them.
>
> I see 3 ways to do this:
>
> - have the parser provide hooks to subclass the various types of nodes
> that it creates which is not usually the case...
> - take a good look at the parser, subclass it, and re-write ALL the
> methods that create nodes, which seems like a lot of work for what I
> want to do,
> - mess with namespaces and add my own new methods to the original node
> package(s), which I often do, but strikes me as not totally kosher
> (and in any case it makes changing the constructor of the nodes a bit
> awkward)
>
> So I thought about it, looked around CPAN to figure out if I could find
> something that would do this, did not find it, and set out to write it
> myself.
Did you try using delegation instead of subclassing? See, for
example, Class::Delegate on CPAN.
- Kurt