{
    "mode": "perldoc",
    "parameter": "Moose::Cookbook::Meta::PrivateOrPublic_MethodMetaclass",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Moose%3A%3ACookbook%3A%3AMeta%3A%3APrivateOrPublic_MethodMetaclass/json",
    "generated": "2026-07-05T23:47:01Z",
    "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": {
        "NAME": {
            "content": "Moose::Cookbook::Meta::PrivateOrPublicMethodMetaclass - A method metaclass for marking methods\npublic 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 private methods. If a\nmethod is defined as private, it adds a wrapper around the method which dies unless it is called\nfrom the class where it was defined.\n\nThe way the method is added to the class is rather ugly. If we wanted to make this a real\nfeature, we'd probably want to add some sort of sugar to allow us to declare private methods,\nbut that is beyond the scope of this recipe. See the Extending recipes for more on this topic.\n\nThe core of our custom class is the \"policy\" attribute, and \"addpolicywrapper\" method.\n\nYou'll note that we have to explicitly set the \"policy\" attribute in our constructor:\n\n$self->{policy} = $options{policy};\n\nThat is necessary because Moose metaclasses do not use the meta API to create objects. Most\nMoose classes have a custom \"inlined\" constructor for speed.\n\nIn this particular case, our parent class's constructor is the \"wrap\" method. We call that to\nbuild our object, but it does not include subclass-specific attributes.\n\nThe \"addpolicywrapper\" method is where the real work is done. If the method is private, we\nconstruct a wrapper around the real subroutine which checks that the caller matches the package\nin which the subroutine was created.\n\nIf they don't match, it dies. If they do match, the real method is called. We use \"goto\" so that\nthe wrapper does not show up in the call stack.\n\nFinally, we replace the value of \"$self->{body}\". This is another case where we have to do\nsomething a bit gross because Moose does not use Moose for its own implementation.\n\nWhen we pass this method object to the metaclass's \"addmethod\" method, it will take the method\nbody and make it available in the class.\n\nFinally, when we retrieve these methods via the introspection API, we can call the \"ispublic\"\nand \"isprivate\" methods on them to get more information about the method.\n",
            "subsections": []
        },
        "SUMMARY": {
            "content": "A custom method metaclass lets us add both behavior and meta-information to methods.\nUnfortunately, because the Perl interpreter does not provide easy hooks into method declaration,\nthe API we have for adding these methods is not very pretty.\n\nThat can be improved with custom Moose-like sugar, or even by using a tool like Devel::Declare\nto create full-blown new keywords in Perl.\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::Meta::PrivateOrPublicMethodMetaclass - A method metaclass for marking methods public or private",
    "flags": [],
    "examples": [],
    "see_also": []
}