{
    "mode": "perldoc",
    "parameter": "Moose::Cookbook::Roles::Restartable_AdvancedComposition",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Moose%3A%3ACookbook%3A%3ARoles%3A%3ARestartable_AdvancedComposition/json",
    "generated": "2026-10-22T12:13:22Z",
    "synopsis": "package Restartable;\nuse Moose::Role;\nhas 'ispaused' => (\nis      => 'rw',\nisa     => 'Bool',\ndefault => 0,\n);\nrequires 'savestate', 'loadstate';\nsub stop { 1 }\nsub start { 1 }\npackage Restartable::ButUnreliable;\nuse Moose::Role;\nwith 'Restartable' => {\n-alias => {\nstop  => 'stop',\nstart => 'start'\n},\n-excludes => [ 'stop', 'start' ],\n};\nsub stop {\nmy $self = shift;\n$self->explode() if rand(1) > .5;\n$self->stop();\n}\nsub start {\nmy $self = shift;\n$self->explode() if rand(1) > .5;\n$self->start();\n}\npackage Restartable::ButBroken;\nuse Moose::Role;\nwith 'Restartable' => { -excludes => [ 'stop', 'start' ] };\nsub stop {\nmy $self = shift;\n$self->explode();\n}\nsub start {\nmy $self = shift;\n$self->explode();\n}",
    "sections": {
        "NAME": {
            "content": "Moose::Cookbook::Roles::RestartableAdvancedComposition - Advanced Role Composition - method\nexclusion and aliasing\n",
            "subsections": []
        },
        "VERSION": {
            "content": "version 2.2207\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "package Restartable;\nuse Moose::Role;\n\nhas 'ispaused' => (\nis      => 'rw',\nisa     => 'Bool',\ndefault => 0,\n);\n\nrequires 'savestate', 'loadstate';\n\nsub stop { 1 }\n\nsub start { 1 }\n\npackage Restartable::ButUnreliable;\nuse Moose::Role;\n\nwith 'Restartable' => {\n-alias => {\nstop  => 'stop',\nstart => 'start'\n},\n-excludes => [ 'stop', 'start' ],\n};\n\nsub stop {\nmy $self = shift;\n\n$self->explode() if rand(1) > .5;\n\n$self->stop();\n}\n\nsub start {\nmy $self = shift;\n\n$self->explode() if rand(1) > .5;\n\n$self->start();\n}\n\npackage Restartable::ButBroken;\nuse Moose::Role;\n\nwith 'Restartable' => { -excludes => [ 'stop', 'start' ] };\n\nsub stop {\nmy $self = shift;\n\n$self->explode();\n}\n\nsub start {\nmy $self = shift;\n\n$self->explode();\n}\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "In this example, we demonstrate how to exercise fine-grained control over what methods we\nconsume from a role. We have a \"Restartable\" role which provides an \"ispaused\" attribute, and\ntwo methods, \"stop\" and \"start\".\n\nThen we have two more roles which implement the same interface, each putting their own spin on\nthe \"stop\" and \"start\" methods.\n\nIn the \"Restartable::ButUnreliable\" role, we want to provide a new implementation of \"stop\" and\n\"start\", but still have access to the original implementation. To do this, we alias the methods\nfrom \"Restartable\" to private methods, and provide wrappers around the originals (1).\n\nNote that aliasing simply *adds* a name, so we also need to exclude the methods with their\noriginal names.\n\nwith 'Restartable' => {\n-alias => {\nstop  => 'stop',\nstart => 'start'\n},\n-excludes => [ 'stop', 'start' ],\n};\n\nIn the \"Restartable::ButBroken\" role, we want to provide an entirely new behavior for \"stop\" and\n\"start\". We exclude them entirely when composing the \"Restartable\" role into\n\"Restartable::ButBroken\".\n\nIt's worth noting that the \"-excludes\" parameter also accepts a single string as an argument if\nyou just want to exclude one method.\n\nwith 'Restartable' => { -excludes => [ 'stop', 'start' ] };\n",
            "subsections": []
        },
        "CONCLUSION": {
            "content": "Exclusion and renaming are a power tool that can be handy, especially when building roles out of\nother roles. In this example, all of our roles implement the \"Restartable\" role. Each role\nprovides same API, but each has a different implementation under the hood.\n\nYou can also use the method aliasing and excluding features when composing a role into a class.\n",
            "subsections": []
        },
        "FOOTNOTES": {
            "content": "(1) The mention of wrapper should tell you that we could do the same thing using method\nmodifiers, but for the sake of this example, we don't.\n",
            "subsections": []
        },
        "AUTHORS": {
            "content": "*   Stevan Little <stevan@cpan.org>\n\n*   Dave Rolsky <autarch@urth.org>\n\n*   Jesse Luehrs <doy@cpan.org>\n\n*   Shawn M Moore <sartak@cpan.org>\n\n*   יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>\n\n*   Karen Etheridge <ether@cpan.org>\n\n*   Florian Ragwitz <rafl@debian.org>\n\n*   Hans Dieter Pearcey <hdp@cpan.org>\n\n*   Chris Prather <chris@prather.org>\n\n*   Matt S Trout <mstrout@cpan.org>\n",
            "subsections": []
        },
        "COPYRIGHT AND LICENSE": {
            "content": "This software is copyright (c) 2006 by Infinity Interactive, Inc.\n\nThis is free software; you can redistribute it and/or modify it under the same terms as the Perl\n5 programming language system itself.\n",
            "subsections": []
        }
    },
    "summary": "Moose::Cookbook::Roles::RestartableAdvancedComposition - Advanced Role Composition - method exclusion and aliasing",
    "flags": [],
    "examples": [],
    "see_also": []
}