{
    "mode": "perldoc",
    "parameter": "Class::Method::Modifiers",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Class%3A%3AMethod%3A%3AModifiers/json",
    "generated": "2026-06-09T14:07:13Z",
    "synopsis": "package Child;\nuse parent 'MyParent';\nuse Class::Method::Modifiers;\nsub newmethod { }\nbefore 'oldmethod' => sub {\ncarp \"oldmethod is deprecated, use newmethod\";\n};\naround 'othermethod' => sub {\nmy $orig = shift;\nmy $ret = $orig->(@);\nreturn $ret =~ /\\d/ ? $ret : lc $ret;\n};\nafter 'private', 'protected' => sub {\ndebug \"finished calling a dangerous method\";\n};\nuse Class::Method::Modifiers qw(fresh);\nfresh 'notinhierarchy' => sub {\nwarn \"freshly added method\\n\";\n};",
    "sections": {
        "NAME": {
            "content": "Class::Method::Modifiers - Provides Moose-like method modifiers\n",
            "subsections": []
        },
        "VERSION": {
            "content": "version 2.13\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "package Child;\nuse parent 'MyParent';\nuse Class::Method::Modifiers;\n\nsub newmethod { }\n\nbefore 'oldmethod' => sub {\ncarp \"oldmethod is deprecated, use newmethod\";\n};\n\naround 'othermethod' => sub {\nmy $orig = shift;\nmy $ret = $orig->(@);\nreturn $ret =~ /\\d/ ? $ret : lc $ret;\n};\n\nafter 'private', 'protected' => sub {\ndebug \"finished calling a dangerous method\";\n};\n\nuse Class::Method::Modifiers qw(fresh);\n\nfresh 'notinhierarchy' => sub {\nwarn \"freshly added method\\n\";\n};\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "Method modifiers are a convenient feature from the CLOS (Common Lisp Object System) world.\n\nIn its most basic form, a method modifier is just a method that calls \"$self->SUPER::foo(@)\". I\nfor one have trouble remembering that exact invocation, so my classes seldom re-dispatch to\ntheir base classes. Very bad!\n\n\"Class::Method::Modifiers\" provides three modifiers: \"before\", \"around\", and \"after\". \"before\"\nand \"after\" are run just before and after the method they modify, but can not really affect that\noriginal method. \"around\" is run in place of the original method, with a hook to easily call\nthat original method. See the \"MODIFIERS\" section for more details on how the particular\nmodifiers work.\n\nOne clear benefit of using \"Class::Method::Modifiers\" is that you can define multiple modifiers\nin a single namespace. These separate modifiers don't need to know about each other. This makes\ntop-down design easy. Have a base class that provides the skeleton methods of each operation,\nand have plugins modify those methods to flesh out the specifics.\n\nParent classes need not know about \"Class::Method::Modifiers\". This means you should be able to\nmodify methods in *any* subclass. See Term::VT102::ZeroBased for an example of subclassing with\n\"Class::Method::Modifiers\".\n\nIn short, \"Class::Method::Modifiers\" solves the problem of making sure you call\n\"$self->SUPER::foo(@)\", and provides a cleaner interface for it.\n\nAs of version 1.00, \"Class::Method::Modifiers\" is faster in some cases than Moose. See\nbenchmark/methodmodifiers.pl in the Moose distribution.\n\n\"Class::Method::Modifiers\" also provides an additional \"modifier\" type, \"fresh\"; see below.\n",
            "subsections": []
        },
        "MODIFIERS": {
            "content": "All modifiers let you modify one or multiple methods at a time. The names of multiple methods\ncan be provided as a list or as an array-reference. Examples:\n\nbefore 'method' => sub { ... };\nbefore 'method1', 'method2' => sub { ... };\nbefore [ 'method1', 'method2' ] => sub { ... };\n\nbefore method(s) => sub { ... };\n\"before\" is called before the method it is modifying. Its return value is totally ignored. It\nreceives the same @ as the method it is modifying would have received. You can modify the @\nthe original method will receive by changing $[0] and friends (or by changing anything inside a\nreference). This is a feature!\n\nafter method(s) => sub { ... };\n\"after\" is called after the method it is modifying. Its return value is totally ignored. It\nreceives the same @ as the method it is modifying received, mostly. The original method can\nmodify @ (such as by changing $[0] or references) and \"after\" will see the modified version.\nIf you don't like this behavior, specify both a \"before\" and \"after\", and copy the @ during\n\"before\" for \"after\" to use.\n\naround method(s) => sub { ... };\n\"around\" is called instead of the method it is modifying. The method you're overriding is passed\nin as the first argument (called $orig by convention). Watch out for contextual return values of\n$orig.\n\nYou can use \"around\" to:\n\nPass $orig a different @\naround 'method' => sub {\nmy $orig = shift;\nmy $self = shift;\n$orig->($self, reverse @);\n};\n\nMunge the return value of $orig\naround 'method' => sub {\nmy $orig = shift;\nucfirst $orig->(@);\n};\n\nAvoid calling $orig -- conditionally\naround 'method' => sub {\nmy $orig = shift;\nreturn $orig->(@) if time() % 2;\nreturn \"no dice, captain\";\n};\n\nfresh method(s) => sub { ... };\n(Available since version 2.00)\n\nUnlike the other modifiers, this does not modify an existing method. Ordinarily, \"fresh\" merely\ninstalls the coderef as a method in the appropriate class; but if the class hierarchy already\ncontains a method of the same name, an exception is thrown. The idea of this \"modifier\" is to\nincrease safety when subclassing. Suppose you're writing a subclass of a class Some::Base, and\nadding a new method:\n\npackage My::Subclass;\nuse base 'Some::Base';\n\nsub foo { ... }\n\nIf a later version of Some::Base also adds a new method named \"foo\", your method will shadow\nthat method. Alternatively, you can use \"fresh\" to install the additional method into your\nsubclass:\n\npackage My::Subclass;\nuse base 'Some::Base';\n\nuse Class::Method::Modifiers 'fresh';\n\nfresh 'foo' => sub { ... };\n\nNow upgrading Some::Base to a version with a conflicting \"foo\" method will cause an exception to\nbe thrown; seeing that error will give you the opportunity to fix the problem (perhaps by\npicking a different method name in your subclass, or similar).\n\nCreating fresh methods with \"installmodifier\" (see below) provides a way to get similar safety\nbenefits when adding local monkeypatches to existing classes; see\n<http://aaroncrane.co.uk/talks/monkeypatchingsubclassing/>.\n\nFor API compatibility reasons, this function is exported only when you ask for it specifically,\nor for \":all\".\n\ninstallmodifier $package, $type, @names, sub { ... }\n\"installmodifier\" is like \"before\", \"after\", \"around\", and \"fresh\" but it also lets you\ndynamically select the modifier type ('before', 'after', 'around', 'fresh') and package that the\nmethod modifiers are installed into. This expert-level function is exported only when you ask\nfor it specifically, or for \":all\".\n",
            "subsections": []
        },
        "NOTES": {
            "content": "All three normal modifiers; \"before\", \"after\", and \"around\"; are exported into your namespace by\ndefault. You may \"use Class::Method::Modifiers ()\" to avoid modifying your namespace. I may\nsteal more features from Moose, namely \"super\", \"override\", \"inner\", \"augment\", and whatever the\nMoose folks come up with next.\n\nNote that the syntax and semantics for these modifiers is directly borrowed from Moose (the\nimplementations, however, are not).\n\nClass::Trigger shares a few similarities with \"Class::Method::Modifiers\", and they even have\nsome overlap in purpose -- both can be used to implement highly pluggable applications. The\ndifference is that Class::Trigger provides a mechanism for easily letting parent classes to\ninvoke hooks defined by other code. \"Class::Method::Modifiers\" provides a way of\noverriding/augmenting methods safely, and the parent class need not know about it.\n\n:lvalue METHODS\nWhen adding \"before\" or \"after\" modifiers, the wrapper method will be an lvalue method if the\nwrapped sub is, and assigning to the method will propagate to the wrapped method as expected.\nFor \"around\" modifiers, it is the modifier sub that determines if the wrapper method is an\nlvalue method.\n",
            "subsections": []
        },
        "CAVEATS": {
            "content": "It is erroneous to modify a method that doesn't exist in your class's inheritance hierarchy. If\nthis occurs, an exception will be thrown when the modifier is defined.\n\nIt doesn't yet play well with \"caller\". There are some \"TODO\" tests for this. Don't get your\nhopes up though!\n\nApplying modifiers to array lvalue methods is not fully supported. Attempting to assign to an\narray lvalue method that has an \"after\" modifier applied will result in an error. Array lvalue\nmethods are not well supported by perl in general, and should be avoided.\n",
            "subsections": []
        },
        "MAJOR VERSION CHANGES": {
            "content": "This module was bumped to 1.00 following a complete reimplementation, to indicate breaking\nbackwards compatibility. The \"guard\" modifier was removed, and the internals are completely\ndifferent.\n\nThe new version is a few times faster with half the code. It's now even faster than Moose.\n\nAny code that just used modifiers should not change in behavior, except to become more correct.\nAnd, of course, faster. :)\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "*   Class::Method::Modifiers::Fast\n\n*   Moose\n\n*   Class::Trigger\n\n*   Class::MOP::Method::Wrapped\n\n*   MRO::Compat\n\n*   CLOS <https://en.wikipedia.org/wiki/CommonLispObjectSystem>\n",
            "subsections": []
        },
        "ACKNOWLEDGEMENTS": {
            "content": "Thanks to Stevan Little for Moose, I would never have known about method modifiers otherwise.\n\nThanks to Matt Trout and Stevan Little for their advice.\n",
            "subsections": []
        },
        "SUPPORT": {
            "content": "Bugs may be submitted through the RT bug tracker\n<https://rt.cpan.org/Public/Dist/Display.html?Name=Class-Method-Modifiers> (or\nbug-Class-Method-Modifiers@rt.cpan.org <mailto:bug-Class-Method-Modifiers@rt.cpan.org>).\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Shawn M Moore <sartak@gmail.com>\n",
            "subsections": []
        },
        "CONTRIBUTORS": {
            "content": "*   Karen Etheridge <ether@cpan.org>\n\n*   Shawn M Moore <code@sartak.org>\n\n*   Graham Knop <haarg@haarg.org>\n\n*   Aaron Crane <arc@cpan.org>\n\n*   Peter Rabbitson <ribasushi@cpan.org>\n\n*   Justin Hunter <justin.d.hunter@gmail.com>\n\n*   David Steinbrunner <dsteinbrunner@pobox.com>\n\n*   gfx <gfuji@cpan.org>\n\n*   mannih <github@lxxi.org>\n",
            "subsections": []
        },
        "COPYRIGHT AND LICENSE": {
            "content": "This software is copyright (c) 2007 by Shawn M Moore.\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": "Class::Method::Modifiers - Provides Moose-like method modifiers",
    "flags": [],
    "examples": [],
    "see_also": []
}