How to force all inherited methods to return subclass objects?

I am writing hOCR::Element package on top of HTML::Element to handle hOCR . Accordingly, an object of class hOCR::Element inherits all methods belonging to HTML::Element .

But since there are a number of essential methods from HTML::Element (such as find_by_tag_name and look_down ) that return HTML::Element objects, I see that I have to write the corresponding hOCR::Element wrapper for each such method, which simply displays by a blessing like hOCR is that it is given as HTML .

For instance:

 package hOCR::Element; use HTML::Element; use parent 'HTML::Element'; sub new { my ($class, %params) = @_; my $self = $class->SUPER::new (%params); return bless $self, $class; } sub look_down { my $self = shift; return map { bless $_, 'hOCR::Element' } $self->SUPER::look_down (@_); } 1; 

How can I subclasses inherit all methods from my base class so that these corresponding methods return subclass objects instead of base class objects, without having to write a wrapper for each such parent method?

+5
source share
1 answer
 sub look_down { my $self = shift; return bless $self->SUPER::look_down (@_) ,HTML::Element ; } 
-2
source

Source: https://habr.com/ru/post/1241890/


All Articles