Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: object oriented Perl advice / constructor creation in child class

by Anonymous Monk
on Jul 10, 2018 at 09:13 UTC ( #1218231=note: print w/replies, xml ) Need Help??


in reply to object oriented Perl advice / constructor creation in child class

From a design point of view, this looks like a task for Module::Pluggable. If all your modules A, B, C, D provide similar interface, you could just iterate over $self->plugins in your methods instead of spelling out special cases for A, B, C, D manually. Untested pseudo-Perl example:

package Synergy; use Module::Pluggable instantiate => 'new'; sub new { my ($class, @opts) = @_; return bless [ $class->plugins(@opts) ], $class; # now your object contains objects of classes A, B, C, D # and whatever else was in Synergy/*.pm near Synergy.pm } sub process { my ($self, @args) = @_; return map { $_->process(@args) } @$self; # passes args to all sub-modules }

On the other hand, if your modules all provide different methods with no intersection whatsoever and you just need to have all of them in one class, you could try to use base to inherit all their methods automatically. This way you only need to spell out your parent packages once.

But if you need to call different parent methods from the same method depending on other circumstances... That sounds like a very complex wrapper. Maybe what you are trying to achieve can be achieved in a simpler way?

Replies are listed 'Best First'.
Re^2: object oriented Perl advice / constructor creation in child class
by smarthacker67 (Beadle) on Jul 11, 2018 at 06:48 UTC
    Hi Sir,

    Thanks for your answer All these A,B,C & D modules have method new so will that cause some conflicts?

      Yes, calling multiple inherited SUPER::new() constructors would be a problem (I don't think mro would help, either), which is why I asked you for more details about modules you are trying to put together.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1218231]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2023-09-26 19:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?