Dallaylaen has asked for the wisdom of the Perl Monks concerning the following question:
Hello, dear esteemed monks!
I just wrote the following in my module. The idea is as follows: allow user to override default methods by either subclassing, or providing a callback. So a subclass with do_foo method and an instance of base class with on_foo member will behave exactly the same.
However, looking at this again, I suspect it's being overengineered. Should I just provide default do_foo methods in subclass that search for a callback and croak if none found? This will still allow for both ad-hoc overriding and subclassing.
Should I just go with normal OO and leave do_foo() alone?
Guess there's no single correct answer after all, but I'd like to hear your opinions and make up my own.
sub backend_call { my $self = shift; my $method = shift; my $todo = $self->{"on_$method"} || $self->can("do_$method"); if (!$todo) { my $sub = [caller(1)]->[3]; $sub =~ s/.*:://; croak join "", (ref $self || $self),"->",$sub, ": no backend found for $method"; }; return $todo->($self, @_); };
Thank you!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Implementing methods in a subclass or providing in-place callback: Is it overengineered?
by haukex (Archbishop) on Sep 23, 2016 at 09:51 UTC | |
by Dallaylaen (Chaplain) on Sep 23, 2016 at 18:03 UTC | |
|
Re: Implementing methods in a subclass or providing in-place callback: Is it overengineered?
by BrowserUk (Patriarch) on Sep 23, 2016 at 12:27 UTC |