in reply to Re: Remove roles for an object in perl moose
in thread Remove roles for an object in perl moose

So is delegation a form of composition where the object exposes the api of one of its composite participants?


Dave

  • Comment on Re^2: Remove roles for an object in perl moose

Replies are listed 'Best First'.
Re^3: Remove roles for an object in perl moose
by tobyink (Canon) on Sep 28, 2013 at 21:44 UTC

    The handles => ['teach'] bit is really just a shortcut for writing something like:

    sub teach { (shift->teacher or die)->teach(@_); }

    It's not especially sophisticated - e.g. it doesn't make can("teach") return false if teacher is not set. But it's a useful technique for observing the Law of Demeter.

    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
      Hi Tobyink,

      Thanks fr such a wonderful

      reply. The problem actually is, lets assume we ve two modules named car.pm and bike.pm both contain same methods with exactly same names but different functionalities . There is a super class called vehicle.pm which actually decides methods in car.pm should be called or bike.pm should be called based on certain param from the calling script.

      this switching should be dynamic like in run time we should be able decide to switch between car and bike objects . So we have decided to assign roles to vehicle.pm to switch roles to Car and bike and one role being at a time. So only there was a need to remove one role when other s active. Please comment

        You want to model vehicles that can convert themselves into other vehicles? Like the batmobile?

        Because if not, then you're going about this the wrong way. You shouldn't be converting cars into bikes and vice versa. You should just have car objects and bike objects.

        Further, if seems you have Vehicle as a class and Car and Bike as roles. That should be the other way around; Vehicle should be a role that Car and Bike both do.

        use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name