Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Remove roles for an object in perl moose

by Anonymous Monk
on Sep 28, 2013 at 12:13 UTC ( #1056122=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi , Is there any feature in perl moose to remove the role assigned for an object in a similar way we are applying the role , so that the methods related to the role are not callable through the object ? Thanks, Bala.

Comment on Remove roles for an object in perl moose
Re: Remove roles for an object in perl moose (designflaw)
by Anonymous Monk on Sep 28, 2013 at 12:24 UTC

    Its possible, but they say its a design flaw to do it

    *foo::bar::doesrole = sub{die 'no can do no more'};

Re: Remove roles for an object in perl moose
by tobyink (Abbot) on Sep 28, 2013 at 12:41 UTC

    If the role has been applied to the instance (rather than its class), then you can certainly remove all such roles by reblessing it back into its original class:

    $class->meta->rebless_instance($object);

    But this sounds like a bad idea.

    Perhaps instead of adding and removing roles you should consider using delegation.

    class Teacher { method teach () { ... } } class ClassRoom { has teacher => ( is => 'rw', handles => ['teach'], clearer => 'vacate', ); } my $room1 = ClassRoom->new; eval { $room1->teach; # dies (no teacher!) }; # Instead of adding a role, # give the classroom an object # that it can delegate to... # $room1->teacher( Teacher->new ); $room1->teach; # ok! # Instead of removing the role, # remove the object that's being # delegated to... $room1->vacate; eval { $room1->teach; # dies (no teacher!) };

    Roles are awesome and it's tempting to use them for everything, but maybe delegation is something more appropriate for the problem you're trying to solve. Step back and explain your wider problem and we might be able to give you other ideas of how to approach it.

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

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


      Dave

        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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1056122]
Approved by LanX
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2014-09-22 07:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (182 votes), past polls