![]() |
|
No such thing as a small change | |
PerlMonks |
Re: object oriented Perl advice / constructor creation in child classby haj (Vicar) |
on Jul 10, 2018 at 09:09 UTC ( #1218227=note: print w/replies, xml ) | Need Help?? |
Well, your example doesn't actually show inheritance. Your constructor creates a "has-a" relationship, because your C1 objects will have one object of each of the classes A, B, C and D as attributes. This is a solid pattern in many cases, but it doesn't give you direct access to the methods of the four classes. I'd say that today's perlish way to do OOP is Moose or its lightweight cousin Moo, and I definitely recommend those if you have several classes to inherit from. Here's a short example:
Now you can do things like this:
So, your C1 objects have direct access to both the say_hello method from A.pm, and the say_goodbye method from C.pm.
Note that Moo (and Moose) will take care for object constructors: You don't write new methods! If you want to do object inheritance without an object framework, you do it like this: However, in that case you need to take care for calling parent constructors and conflict resolution yourself, which is why I don't expand on this.
In Section
Seekers of Perl Wisdom
|
|