{
    "mode": "info",
    "parameter": "Moose::Cookbook::Roles::Restartable_AdvancedComposition",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/info/Moose%3A%3ACookbook%3A%3ARoles%3A%3ARestartable_AdvancedComposition/json",
    "generated": "2026-07-05T12:59:17Z",
    "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": {
        "Moose::Cookbook::RMoose::Cookbook::Roles::RestartableAdvancedComposition(3pm)": {
            "content": "",
            "subsections": []
        },
        "NAME": {
            "content": "Moose::Cookbook::Roles::RestartableAdvancedComposition - Advanced Role\nComposition - method exclusion and aliasing\n",
            "subsections": []
        },
        "VERSION": {
            "content": "version 2.2200\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\nover what methods we consume from a role. We have a \"Restartable\" role\nwhich provides an \"ispaused\" attribute, and two methods, \"stop\" and\n\"start\".\n\nThen we have two more roles which implement the same interface, each\nputting their own spin on the \"stop\" and \"start\" methods.\n\nIn the \"Restartable::ButUnreliable\" role, we want to provide a new\nimplementation of \"stop\" and \"start\", but still have access to the\noriginal implementation. To do this, we alias the methods from\n\"Restartable\" to private methods, and provide wrappers around the\noriginals (1).\n\nNote that aliasing simply adds a name, so we also need to exclude the\nmethods with their original 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\nnew behavior for \"stop\" and \"start\". We exclude them entirely when\ncomposing the \"Restartable\" role into \"Restartable::ButBroken\".\n\nIt's worth noting that the \"-excludes\" parameter also accepts a single\nstring as an argument if you 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\nwhen building roles out of other roles. In this example, all of our\nroles implement the \"Restartable\" role. Each role provides same API,\nbut each has a different implementation under the hood.\n\nYou can also use the method aliasing and excluding features when\ncomposing a role into a class.\n",
            "subsections": []
        },
        "FOOTNOTES": {
            "content": "(1) The mention of wrapper should tell you that we could do the same\nthing using method modifiers, but for the sake of this example, we\ndon't.\n",
            "subsections": []
        },
        "AUTHORS": {
            "content": "o   Stevan Little <stevan@cpan.org>\n\no   Dave Rolsky <autarch@urth.org>\n\no   Jesse Luehrs <doy@cpan.org>\n\no   Shawn M Moore <sartak@cpan.org>\n\no    ' (Yuval Kogman) <nothingmuch@woobling.org>\n\no   Karen Etheridge <ether@cpan.org>\n\no   Florian Ragwitz <rafl@debian.org>\n\no   Hans Dieter Pearcey <hdp@cpan.org>\n\no   Chris Prather <chris@prather.org>\n\no   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\nthe same terms as the Perl 5 programming language system itself.\n\nperl v5.34.0      Moose::Cookbook::Roles::RestartableAdvancedComposition(3pm)",
            "subsections": []
        }
    },
    "summary": "Moose::Cookbook::Roles::RestartableAdvancedComposition - Advanced Role Composition - method exclusion and aliasing",
    "flags": [],
    "examples": [],
    "see_also": []
}