Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

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

Replies are listed 'Best First'.
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:


    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?


        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
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'};

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1056122]
Approved by LanX
[Lady_Aleena]: Having to rewrite some parts of my site for mobile devices is such a pain in the butt.
[shmem]: Lady_Aleena: what editor are you using, on what platform?
[Lady_Aleena]: shmem, geany on Debian jessie.
[shmem]: some experience with vim?
[Lady_Aleena]: None
[shmem]: I don't know geany, and don't know whether it has support for ctags.
[Lady_Aleena]: ctags?
[shmem]: ctags is a program which (recursively) extracts the symbols from source and stores them in a one-file database. This allows you to query the locations where these symbols (e.g. a subroutine name) are used anywhere in the source code tree...
[shmem]: ...from inside the editor.

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2017-04-27 12:05 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (505 votes). Check out past polls.