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

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

Replies are listed 'Best First'.
Re^4: Remove roles for an object in perl moose
by Anonymous Monk on Sep 29, 2013 at 18:23 UTC
    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
        Hi , Somehow, it is like batmobiles only. In the delegation apporach you suggested, is it possible in any way to add all the methods of the class teacher entirely instead of adding all api's. Instead , can we add the entire class to the handles, so that all the methods within the class is accesbile through the classroom object apart from 'teach' method. Thanks, Bala.