Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Extending object you don't create

by dcorbin (Sexton)
on Feb 28, 2001 at 06:13 UTC ( #61256=perlquestion: print w/ replies, xml ) Need Help??
dcorbin has asked for the wisdom of the Perl Monks concerning the following question:

HTML::TreeBuilder construct a tree of HTML::Element objects. I have some routines that should naturally (from an OO perspective) be part of HTML::Element. Since TreeBuilder creates these, and doesn't use a Factory Pattern (in which case I would derive from HTML::Element), is there some neat perlish way to add methods to HTML::Element? Please give me an example. Thanks.

Comment on Extending object you don't create
Re: Extending object you don't create
by dws (Chancellor) on Feb 28, 2001 at 06:29 UTC
    How about re-blessing?

    Let HTML::TreeBuilder construct the tree, then re-bless instances of HTML::Element to be instances of your subclass (of HTML::Element).

    Or, if you need to do this during construction, consider subclassing HTML::TreeBuilder and selectively overriding methods.

      Or, if you need to do this during construction, consider subclassing HTML::TreeBuilder and selectively overriding methods.
      That won't work if HTML::TreeBuilder has "hard coded" the name of HTML::Element, which I suspect it has.

      As a hack, I'd just define methods in HTML::Element's space.

      For long term, write the author of HTML::TreeBuilder and tell him to make the subclass an overrideable constant:

      package HTML::TreeBuilder; sub element_class { return "HTML::Element"; } ... sub make_sub_node { ... my $ele = $self->element_class->new(...); #makes an HTML::Element pe +rhaps .. }
      because then you can override that:
      package My::TreeBuilder; use base qw(HTML::TreeBuilder); sub element_class { return "My::Element" }; package My::Element; use base qw(HTML::Element); sub my_additional_method { ... }
      .

      -- Randal L. Schwartz, Perl hacker

        This is easier to pull off than that I thought. HTML::TreeBuilder already makes allowance for building a tree with objects other than instance of HTML::Element.
        my $tree = new HTML::TreeBuilder(...); # now replace 'HTML::Element' with my subclass $tree->{'_element_class'} = 'HTML::MyElementSubclass';

        Or, if this offends your sensibilities, subclass HTML::TreeBuilder, override new(), and invoke INHERITED::new() before resetting the element class.

        This works with HTML::TreeBuilder 3.09.

Re: Extending object you don't create
by dcorbin (Sexton) on Feb 28, 2001 at 07:10 UTC
    Acutally while dws has suggested the best alternative for THIS case, I'm interested in what people might think about the following hack for the more general case: If I put my routines in HTML::ElementEx, and that modify the @HTML::Element::ISA array to contain HTML::ElementEx. Comments?
      If your intent is to subclass HTML::Element, then confusing it about what its superclass is will get you into some interesting trouble. Switching out the superclass impacts all subclasses of HTML::Element, one of which is HTML::Parser

      Stick with Plan A.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://61256]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2014-07-26 17:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (178 votes), past polls