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

Maybe the solution is to precede the subs with some kind of unique identifier for each of the packages that the calling package can look up.

$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

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

Replies are listed 'Best First'.
Re^4: Creating dynamic parent/child relationships
by Corion (Patriarch) on Sep 04, 2019 at 12:57 UTC

    ... 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.

      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?