{
    "mode": "perldoc",
    "parameter": "Moose::Cookbook::Legacy::Debugging_BaseClassReplacement",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Moose%3A%3ACookbook%3A%3ALegacy%3A%3ADebugging_BaseClassReplacement/json",
    "generated": "2026-06-12T18:42:09Z",
    "synopsis": "package MyApp::Base;\nuse Moose;\nextends 'Moose::Object';\nbefore 'new' => sub { warn \"Making a new \" . $[0] };\nno Moose;\npackage MyApp::UseMyBase;\nuse Moose ();\nuse Moose::Exporter;\nMoose::Exporter->setupimportmethods( also => 'Moose' );\nsub initmeta {\nshift;\nreturn Moose->initmeta( @, baseclass => 'MyApp::Base' );\n}",
    "sections": {
        "NAME": {
            "content": "Moose::Cookbook::Legacy::DebuggingBaseClassReplacement - Providing an alternate base object\nclass\n",
            "subsections": []
        },
        "VERSION": {
            "content": "version 2.2200\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "package MyApp::Base;\nuse Moose;\n\nextends 'Moose::Object';\n\nbefore 'new' => sub { warn \"Making a new \" . $[0] };\n\nno Moose;\n\npackage MyApp::UseMyBase;\nuse Moose ();\nuse Moose::Exporter;\n\nMoose::Exporter->setupimportmethods( also => 'Moose' );\n\nsub initmeta {\nshift;\nreturn Moose->initmeta( @, baseclass => 'MyApp::Base' );\n}\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "WARNING: Replacing the base class entirely, as opposed to applying roles to the base class, is\nstrongly discouraged. This recipe is provided solely for reference when encountering older code\nthat does this.\n\nA common extension is to provide an alternate base class. One way to do that is to make a\n\"MyApp::Base\" and add \"extends 'MyApp::Base'\" to every class in your application. That's pretty\ntedious. Instead, you can create a Moose-alike module that sets the base object class to\n\"MyApp::Base\" for you.\n\nThen, instead of writing \"use Moose\" you can write \"use MyApp::UseMyBase\".\n\nIn this particular example, our base class issues some debugging output every time a new object\nis created, but you can think of some more interesting things to do with your own base class.\n\nThis uses the magic of Moose::Exporter. When we call \"Moose::Exporter->setupimportmethods(\nalso => 'Moose' )\" it builds \"import\" and \"unimport\" methods for you. The \"also => 'Moose'\" bit\nsays that we want to export everything that Moose does.\n\nThe \"import\" method that gets created will call our \"initmeta\" method, passing it \"forcaller\n=> $caller\" as its arguments. The $caller is set to the class that actually imported us in the\nfirst place.\n\nSee the Moose::Exporter docs for more details on its API.\n\nUSING MyApp::UseMyBase\nTo actually use our new base class, we simply use \"MyApp::UseMyBase\" *instead* of \"Moose\". We\nget all the Moose sugar plus our new base class.\n\npackage Foo;\n\nuse MyApp::UseMyBase;\n\nhas 'size' => ( is => 'rw' );\n\nno MyApp::UseMyBase;\n",
            "subsections": []
        },
        "CONCLUSION": {
            "content": "This is an awful lot of magic for a simple base class. You will often want to combine a\nmetaclass trait with a base class extension, and that's when this technique is useful.\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::Legacy::DebuggingBaseClassReplacement - Providing an alternate base object class",
    "flags": [],
    "examples": [],
    "see_also": []
}