Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re^4: Creating dynamic parent/child relationships

by Corion (Pope)
on Sep 04, 2019 at 12:57 UTC ( #11105585=note: print w/replies, xml ) Need Help??

in reply to Re^3: Creating dynamic parent/child relationships
in thread Creating dynamic parent/child relationships

... most likely, Roles are just the wrong tool. Roles are just inheritance in disguise and only make sense when you have mostly identical behaviour that you want to customize in small parts.

What you have is a set of behaviours that you want to arrange in a sequence, potentially leaving out some parts. This is not easily modeled by inheriting things and much easier modelled by an array that contains the steps.

  • Comment on Re^4: Creating dynamic parent/child relationships

Replies are listed 'Best First'.
Re^5: Creating dynamic parent/child relationships
by nysus (Vicar) on Sep 04, 2019 at 13:21 UTC

    Thanks, Corion. Yes, I tried your advice. Unfortunately, it appears my classes are too reliant on inheritance and I can't implement your idea in any sane way. It seems that if I treat the classes as nothing more than containers for methods using your suggestion, I lose access to the data in the attributes contained in the parent classes. My child objects need access to the parent attributes to work.

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

      Consider passing what you call "the parent" around to each object that implements an action? So far you've been somewhat unclear about what you actually need, so it is somewhat hard to come up with good names for the parts that make sense in your context.

      Maybe using "configuration" as name for what you call "parent" and "action" as name for what you call "child" would help?

      my $cfg = My::Config->new_from_file('nysus.yml'); $cfg->process( $myfile ); package My::Config; use Module::Pluggable require => '1', sub_name => 'actions', instantiate => 'new', ; sub My::Config::process( $self, $filename ) { for my $action ($self->actions) { $action->operate( $self, $filename ); }; };

      I don't think that passing data around in $self should be an overarching design principle.

      Most of the code above is from Module::Pluggable, which still feels very applicable from what you've described so far and from the problems you encounter.

      Maybe if you can elaborate more on the concrete problem you're trying to solve and the problems you encounter, we can give you more concrete advice?

        Basically, to get a better familiarity with OO Perl, I'm cutting my teeth and trying to get better at native OO by writing a potential CPAN module (if I can get it to good enough quality), which you can see here: The synopsis should have the needed details.

        $PM = "Perl Monk's";
        $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
        $nysus = $PM . ' ' . $MCF;
        Click here if you love Perl Monks

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2020-11-29 08:35 GMT
Find Nodes?
    Voting Booth?

    No recent polls found