Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Creating dynamic parent/child relationships

by hippo (Bishop)
on Sep 03, 2019 at 09:16 UTC ( [id://11105505]=note: print w/replies, xml ) Need Help??


in reply to Creating dynamic parent/child relationships

So I need some kind of good system to tell package Three when I want it to use package Two sometimes and package Four other times.

Sounds to me like you want to use roles. See eg. Role::Tiny.

  • Comment on Re: Creating dynamic parent/child relationships

Replies are listed 'Best First'.
Re^2: Creating dynamic parent/child relationships
by nysus (Parson) on Sep 04, 2019 at 11:16 UTC

    I'm looking into this possibility now. I've used roles with Moose and so I'm familiar with them. In my particular case, the plugins I want to use contain identical method names. So will that make the use of Roles impossible?

    $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

      Why would it? SSCCE:

      use strict; use warnings; package Foo; use Moo; package Trapezist; use Role::Tiny; sub swing { print "Whoooosh!\n"; } package Trumpeter; use Role::Tiny; sub swing { print "1, 2, ... 1, 2, 3, 4, Hit it!\n"; } package main; my $person1 = Foo->new; Role::Tiny->apply_roles_to_object ($person1, 'Trapezist'); my $person2 = Foo->new; Role::Tiny->apply_roles_to_object ($person2, 'Trumpeter'); $person1->swing; $person2->swing;

        Nice. But how the F all you guys make coming up with these suggestions so effortless is beyond me. :)

        $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

        Well, so far, so good. This approach looks very promising. I've converted one module over so far and it works. Thanks a lot.

        One thing I wanted to accomplish with this module is to get a much better feel for OO Perl. I'm not sure what goes on under the hood with roles. My old Damian Conway book from like 2000 makes no mention of them. You know any good resources for learning about how they accomplish their jobs? I noticed the classes get tacked with a __WITH__ and then the name of the role.

        $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

      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

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-26 04:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found