{
    "mode": "info",
    "parameter": "Moose::Cookbook::Meta::PrivateOrPublic_MethodMetaclass",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/info/Moose%3A%3ACookbook%3A%3AMeta%3A%3APrivateOrPublic_MethodMetaclass/json",
    "generated": "2026-07-05T13:18:38Z",
    "synopsis": "package MyApp::Meta::Method::PrivateOrPublic;\nuse Moose;\nuse Moose::Util::TypeConstraints;\nextends 'Moose::Meta::Method';\nhas 'policy' => (\nis       => 'ro',\nisa      => enum( [ qw( public private ) ] ),\ndefault  => 'public',\ninitarg => 'policy',\n);\nsub new {\nmy $class   = shift;\nmy %options = @;\nmy $self = $class->SUPER::wrap(%options);\n$self->{policy} = $options{policy};\n$self->addpolicywrapper;\nreturn $self;\n}\nsub addpolicywrapper {\nmy $self = shift;\nreturn if $self->ispublic;\nmy $name      = $self->name;\nmy $package   = $self->packagename;\nmy $realbody = $self->body;\nmy $body = sub {\ndie \"The $package\\::$name method is private\"\nunless ( scalar caller() ) eq $package;\ngoto &{$realbody};\n};\n$self->{body} = $body;\n}\nsub ispublic  { $[0]->policy eq 'public' }\nsub isprivate { $[0]->policy eq 'private' }\npackage MyApp::User;\nuse Moose;\nhas 'password' => ( is => 'rw' );\nPACKAGE->meta()->addmethod(\n'resetpassword',\nMyApp::Meta::Method::PrivateOrPublic->new(\nname         => 'resetpassword',\npackagename => PACKAGE,\nbody         => sub { $[0]->password('reset') },\npolicy       => 'private',\n)\n);",
    "sections": {
        "Moose::Cookbook::MeMoose::Cookbook::Meta::PrivateOrPublicMethodMetaclass(3pm)": {
            "content": "",
            "subsections": []
        },
        "NAME": {
            "content": "Moose::Cookbook::Meta::PrivateOrPublicMethodMetaclass - A method\nmetaclass for marking methods public or private\n",
            "subsections": []
        },
        "VERSION": {
            "content": "version 2.2200\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "package MyApp::Meta::Method::PrivateOrPublic;\n\nuse Moose;\nuse Moose::Util::TypeConstraints;\n\nextends 'Moose::Meta::Method';\n\nhas 'policy' => (\nis       => 'ro',\nisa      => enum( [ qw( public private ) ] ),\ndefault  => 'public',\ninitarg => 'policy',\n);\n\nsub new {\nmy $class   = shift;\nmy %options = @;\n\nmy $self = $class->SUPER::wrap(%options);\n\n$self->{policy} = $options{policy};\n\n$self->addpolicywrapper;\n\nreturn $self;\n}\n\nsub addpolicywrapper {\nmy $self = shift;\n\nreturn if $self->ispublic;\n\nmy $name      = $self->name;\nmy $package   = $self->packagename;\nmy $realbody = $self->body;\n\nmy $body = sub {\ndie \"The $package\\::$name method is private\"\nunless ( scalar caller() ) eq $package;\n\ngoto &{$realbody};\n};\n\n$self->{body} = $body;\n}\n\nsub ispublic  { $[0]->policy eq 'public' }\nsub isprivate { $[0]->policy eq 'private' }\n\npackage MyApp::User;\n\nuse Moose;\n\nhas 'password' => ( is => 'rw' );\n\nPACKAGE->meta()->addmethod(\n'resetpassword',\nMyApp::Meta::Method::PrivateOrPublic->new(\nname         => 'resetpassword',\npackagename => PACKAGE,\nbody         => sub { $[0]->password('reset') },\npolicy       => 'private',\n)\n);\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This example shows a custom method metaclass that models public versus\nprivate methods. If a method is defined as private, it adds a wrapper\naround the method which dies unless it is called from the class where\nit was defined.\n\nThe way the method is added to the class is rather ugly. If we wanted\nto make this a real feature, we'd probably want to add some sort of\nsugar to allow us to declare private methods, but that is beyond the\nscope of this recipe. See the Extending recipes for more on this topic.\n\nThe core of our custom class is the \"policy\" attribute, and\n\"addpolicywrapper\" method.\n\nYou'll note that we have to explicitly set the \"policy\" attribute in\nour constructor:\n\n$self->{policy} = $options{policy};\n\nThat is necessary because Moose metaclasses do not use the meta API to\ncreate objects. Most Moose classes have a custom \"inlined\" constructor\nfor speed.\n\nIn this particular case, our parent class's constructor is the \"wrap\"\nmethod. We call that to build our object, but it does not include\nsubclass-specific attributes.\n\nThe \"addpolicywrapper\" method is where the real work is done. If the\nmethod is private, we construct a wrapper around the real subroutine\nwhich checks that the caller matches the package in which the\nsubroutine was created.\n\nIf they don't match, it dies. If they do match, the real method is\ncalled. We use \"goto\" so that the wrapper does not show up in the call\nstack.\n\nFinally, we replace the value of \"$self->{body}\". This is another case\nwhere we have to do something a bit gross because Moose does not use\nMoose for its own implementation.\n\nWhen we pass this method object to the metaclass's \"addmethod\" method,\nit will take the method body and make it available in the class.\n\nFinally, when we retrieve these methods via the introspection API, we\ncan call the \"ispublic\" and \"isprivate\" methods on them to get more\ninformation about the method.\n",
            "subsections": []
        },
        "SUMMARY": {
            "content": "A custom method metaclass lets us add both behavior and meta-\ninformation to methods. Unfortunately, because the Perl interpreter\ndoes not provide easy hooks into method declaration, the API we have\nfor adding these methods is not very pretty.\n\nThat can be improved with custom Moose-like sugar, or even by using a\ntool like Devel::Declare to create full-blown new keywords in Perl.\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::Meta::PrivateOrPublicMethodMetaclass(3pm)",
            "subsections": []
        }
    },
    "summary": "Moose::Cookbook::Meta::PrivateOrPublicMethodMetaclass - A method metaclass for marking methods public or private",
    "flags": [],
    "examples": [],
    "see_also": []
}