in reply to [Solved] Inheritance when subclass passed as a parameter
I see a couple of issues; I think you're mixing some concepts.
As choroba and hippo said, you use useall to locate the class name of the appropriate "plugin", but then don't instantiate an object of that class, therefore template never gets populated. You could change $self->{service} = $plugin to $self->{service} = $plugin->new to fix that. If you go with this suggestion:
You're using the first parameter of Service->new to locate a subclass. Why not just do e.g. Service::XML->new instead? Then, in your subclasses, you'll need to call the superclass constructor, as in my $self = $class->SUPER::new(@_); $self->{template} = ....
Another option would be to set up a factory method. Although it could be called new, IMO that messes with inheritance a bit. So you could set up a method such as Service->create that simply dispatches to the constructor of the appropriate subclass; then each subclass constructor could call the superclass constructor like in my previous point.
Another small issue in your sample code: You pass the constructor two arguments, but only use the first.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Inheritance when subclass passed as a parameter
by davies (Monsignor) on Feb 05, 2020 at 11:09 UTC | |
by haukex (Archbishop) on Feb 05, 2020 at 11:27 UTC | |
by Fletch (Bishop) on Feb 05, 2020 at 16:27 UTC |