{
    "mode": "perldoc",
    "parameter": "Class::MOP::Class",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Class%3A%3AMOP%3A%3AClass/json",
    "generated": "2026-06-11T18:04:40Z",
    "synopsis": "# assuming that class Foo\n# has been defined, you can\n# use this for introspection ...\n# add a method to Foo ...\nFoo->meta->addmethod( 'bar' => sub {...} )\n# get a list of all the classes searched\n# the method dispatcher in the correct order\nFoo->meta->classprecedencelist()\n# remove a method from Foo\nFoo->meta->removemethod('bar');\n# or use this to actually create classes ...\nClass::MOP::Class->create(\n'Bar' => (\nversion      => '0.01',\nsuperclasses => ['Foo'],\nattributes   => [\nClass::MOP::Attribute->new('$bar'),\nClass::MOP::Attribute->new('$baz'),\n],\nmethods => {\ncalculatebar => sub {...},\nconstructbaz => sub {...}\n}\n)\n);",
    "sections": {
        "NAME": {
            "content": "Class::MOP::Class - Class Meta Object\n",
            "subsections": []
        },
        "VERSION": {
            "content": "version 2.2200\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "# assuming that class Foo\n# has been defined, you can\n\n# use this for introspection ...\n\n# add a method to Foo ...\nFoo->meta->addmethod( 'bar' => sub {...} )\n\n# get a list of all the classes searched\n# the method dispatcher in the correct order\nFoo->meta->classprecedencelist()\n\n# remove a method from Foo\nFoo->meta->removemethod('bar');\n\n# or use this to actually create classes ...\n\nClass::MOP::Class->create(\n'Bar' => (\nversion      => '0.01',\nsuperclasses => ['Foo'],\nattributes   => [\nClass::MOP::Attribute->new('$bar'),\nClass::MOP::Attribute->new('$baz'),\n],\nmethods => {\ncalculatebar => sub {...},\nconstructbaz => sub {...}\n}\n)\n);\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "The Class Protocol is the largest and most complex part of the Class::MOP meta-object protocol.\nIt controls the introspection and manipulation of Perl 5 classes, and it can create them as\nwell. The best way to understand what this module can do is to read the documentation for each\nof its methods.\n",
            "subsections": []
        },
        "INHERITANCE": {
            "content": "\"Class::MOP::Class\" is a subclass of Class::MOP::Module.\n",
            "subsections": []
        },
        "METHODS": {
            "content": "",
            "subsections": [
                {
                    "name": "Class construction",
                    "content": "These methods all create new \"Class::MOP::Class\" objects. These objects can represent existing\nclasses or they can be used to create new classes from scratch.\n\nThe metaclass object for a given class is a singleton. If you attempt to create a metaclass for\nthe same class twice, you will just get the existing object.\n\nClass::MOP::Class->create($packagename, %options)\nThis method creates a new \"Class::MOP::Class\" object with the given package name. It accepts\na number of options:\n\n*       version\n\nAn optional version number for the newly created package.\n\n*       authority\n\nAn optional authority for the newly created package. See \"authority\" in\nClass::MOP::Module for more details.\n\n*       superclasses\n\nAn optional array reference of superclass names.\n\n*       methods\n\nAn optional hash reference of methods for the class. The keys of the hash reference\nare method names and values are subroutine references.\n\n*       attributes\n\nAn optional array reference of Class::MOP::Attribute objects.\n\n*       metaname\n\nSpecifies the name to install the \"meta\" method for this class under. If it is not\npassed, \"meta\" is assumed, and if \"undef\" is explicitly given, no meta method will\nbe installed.\n\n*       weaken\n\nIf true, the metaclass that is stored in the global cache will be a weak reference.\n\nClasses created in this way are destroyed once the metaclass they are attached to\ngoes out of scope, and will be removed from Perl's internal symbol table.\n\nAll instances of a class with a weakened metaclass keep a special reference to the\nmetaclass object, which prevents the metaclass from going out of scope while any\ninstances exist.\n\nThis only works if the instance is based on a hash reference, however.\n\nClass::MOP::Class->createanonclass(%options)\nThis method works just like \"Class::MOP::Class->create\" but it creates an \"anonymous\" class.\nIn fact, the class does have a name, but that name is a unique name generated internally by\nthis module.\n\nIt accepts the same \"superclasses\", \"methods\", and \"attributes\" parameters that \"create\"\naccepts.\n\nIt also accepts a \"cache\" option. If this is \"true\", then the anonymous class will be cached\nbased on its superclasses and roles. If an existing anonymous class in the cache has the\nsame superclasses and roles, it will be reused.\n\nAnonymous classes default to \"weaken => 1\" if cache is \"false\", although this can be\noverridden.\n\nClass::MOP::Class->initialize($packagename, %options)\nThis method will initialize a \"Class::MOP::Class\" object for the named package. Unlike\n\"create\", this method *will not* create a new class.\n\nThe purpose of this method is to retrieve a \"Class::MOP::Class\" object for introspecting an\nexisting class.\n\nIf an existing \"Class::MOP::Class\" object exists for the named package, it will be returned,\nand any options provided will be ignored!\n\nIf the object does not yet exist, it will be created.\n\nThe valid options that can be passed to this method are \"attributemetaclass\",\n\"methodmetaclass\", \"wrappedmethodmetaclass\", and \"instancemetaclass\". These are all\noptional, and default to the appropriate class in the \"Class::MOP\" distribution.\n"
                },
                {
                    "name": "Object instance construction and cloning",
                    "content": "These methods are all related to creating and/or cloning object instances.\n\n$metaclass->cloneobject($instance, %params)\nThis method clones an existing object instance. Any parameters you provide are will override\nexisting attribute values in the object.\n\nThis is a convenience method for cloning an object instance, then blessing it into the\nappropriate package.\n\nYou could implement a clone method in your class, using this method:\n\nsub clone {\nmy ($self, %params) = @;\n$self->meta->cloneobject($self, %params);\n}\n\n$metaclass->reblessinstance($instance, %params)\nThis method changes the class of $instance to the metaclass's class.\n\nYou can only rebless an instance into a subclass of its current class. If you pass any\nadditional parameters, these will be treated like constructor parameters and used to\ninitialize the object's attributes. Any existing attributes that are already set will be\noverwritten.\n\nBefore reblessing the instance, this method will call \"reblessinstanceaway\" on the\ninstance's current metaclass. This method will be passed the instance, the new metaclass,\nand any parameters specified to \"reblessinstance\". By default, \"reblessinstanceaway\" does\nnothing; it is merely a hook.\n\n$metaclass->reblessinstanceback($instance)\nDoes the same thing as \"reblessinstance\", except that you can only rebless an instance into\none of its superclasses. Any attributes that do not exist in the superclass will be\ndeinitialized.\n\nThis is a much more dangerous operation than \"reblessinstance\", especially when multiple\ninheritance is involved, so use this carefully!\n\n$metaclass->newobject(%params)\nThis method is used to create a new object of the metaclass's class. Any parameters you\nprovide are used to initialize the instance's attributes. A special \"INSTANCE\" key can\nbe passed to provide an already generated instance, rather than having Class::MOP generate\nit for you. This is mostly useful for using Class::MOP with foreign classes which generate\ninstances using their own constructors.\n\n$metaclass->instancemetaclass\nReturns the class name of the instance metaclass. See Class::MOP::Instance for more\ninformation on the instance metaclass.\n\n$metaclass->getmetainstance\nReturns an instance of the \"instancemetaclass\" to be used in the construction of a new\ninstance of the class.\n"
                },
                {
                    "name": "Informational predicates",
                    "content": "These are a few predicate methods for asking information about the class itself.\n\n$metaclass->isanonclass\nThis returns true if the class was created by calling\n\"Class::MOP::Class->createanonclass\".\n\n$metaclass->ismutable\nThis returns true if the class is still mutable.\n\n$metaclass->isimmutable\nThis returns true if the class has been made immutable.\n\n$metaclass->ispristine\nA class is *not* pristine if it has non-inherited attributes or if it has any generated\nmethods.\n"
                },
                {
                    "name": "Inheritance Relationships",
                    "content": "$metaclass->superclasses(@superclasses)\nThis is a read-write accessor which represents the superclass relationships of the\nmetaclass's class.\n\nThis is basically sugar around getting and setting @ISA.\n\n$metaclass->classprecedencelist\nThis returns a list of all of the class's ancestor classes. The classes are returned in\nmethod dispatch order.\n\n$metaclass->linearizedisa\nThis returns a list based on \"classprecedencelist\" but with all duplicates removed.\n\n$metaclass->subclasses\nThis returns a list of all subclasses for this class, even indirect subclasses.\n\n$metaclass->directsubclasses\nThis returns a list of immediate subclasses for this class, which does not include indirect\nsubclasses.\n"
                },
                {
                    "name": "Method introspection and creation",
                    "content": "These methods allow you to introspect a class's methods, as well as add, remove, or change\nmethods.\n\nDetermining what is truly a method in a Perl 5 class requires some heuristics (aka guessing).\n\nMethods defined outside the package with a fully qualified name (\"sub Package::name { ... }\")\nwill be included. Similarly, methods named with a fully qualified name using Sub::Name or\nSub::Util are also included.\n\nHowever, we attempt to ignore imported functions.\n\nUltimately, we are using heuristics to determine what truly is a method in a class, and these\nheuristics may get the wrong answer in some edge cases. However, for most \"normal\" cases the\nheuristics work correctly.\n\n$metaclass->getmethod($methodname)\nThis will return a Class::MOP::Method for the specified $methodname. If the class does not\nhave the specified method, it returns \"undef\"\n\n$metaclass->hasmethod($methodname)\nReturns a boolean indicating whether or not the class defines the named method. It does not\ninclude methods inherited from parent classes.\n\n$metaclass->getmethodlist\nThis will return a list of method *names* for all methods defined in this class.\n\n$metaclass->addmethod($methodname, $method)\nThis method takes a method name and a subroutine reference, and adds the method to the\nclass.\n\nThe subroutine reference can be a Class::MOP::Method, and you are strongly encouraged to\npass a meta method object instead of a code reference. If you do so, that object gets stored\nas part of the class's method map directly. If not, the meta information will have to be\nrecreated later, and may be incorrect.\n\nIf you provide a method object, this method will clone that object if the object's package\nname does not match the class name. This lets us track the original source of any methods\nadded from other classes (notably Moose roles).\n\n$metaclass->removemethod($methodname)\nRemove the named method from the class. This method returns the Class::MOP::Method object\nfor the method.\n\n$metaclass->methodmetaclass\nReturns the class name of the method metaclass, see Class::MOP::Method for more information\non the method metaclass.\n\n$metaclass->wrappedmethodmetaclass\nReturns the class name of the wrapped method metaclass, see Class::MOP::Method::Wrapped for\nmore information on the wrapped method metaclass.\n\n$metaclass->getallmethods\nThis will traverse the inheritance hierarchy and return a list of all the Class::MOP::Method\nobjects for this class and its parents.\n\n$metaclass->findmethodbyname($methodname)\nThis will return a Class::MOP::Method for the specified $methodname. If the class does not\nhave the specified method, it returns \"undef\"\n\nUnlike \"getmethod\", this method *will* look for the named method in superclasses.\n\n$metaclass->getallmethodnames\nThis will return a list of method *names* for all of this class's methods, including\ninherited methods.\n\n$metaclass->findallmethodsbyname($methodname)\nThis method looks for the named method in the class and all of its parents. It returns every\nmatching method it finds in the inheritance tree, so it returns a list of methods.\n\nEach method is returned as a hash reference with three keys. The keys are \"name\", \"class\",\nand \"code\". The \"code\" key has a Class::MOP::Method object as its value.\n\nThe list of methods is distinct.\n\n$metaclass->findnextmethodbyname($methodname)\nThis method returns the first method in any superclass matching the given name. It is\neffectively the method that \"SUPER::$methodname\" would dispatch to.\n"
                },
                {
                    "name": "Attribute introspection and creation",
                    "content": "Because Perl 5 does not have a core concept of attributes in classes, we can only return\ninformation about attributes which have been added via this class's methods. We cannot discover\ninformation about attributes which are defined in terms of \"regular\" Perl 5 methods.\n\n$metaclass->getattribute($attributename)\nThis will return a Class::MOP::Attribute for the specified $attributename. If the class\ndoes not have the specified attribute, it returns \"undef\".\n\nNOTE that getattribute does not search superclasses, for that you need to use\n\"findattributebyname\".\n\n$metaclass->hasattribute($attributename)\nReturns a boolean indicating whether or not the class defines the named attribute. It does\nnot include attributes inherited from parent classes.\n\n$metaclass->getattributelist\nThis will return a list of attributes *names* for all attributes defined in this class. Note\nthat this operates on the current class only, it does not traverse the inheritance\nhierarchy.\n\n$metaclass->getallattributes\nThis will traverse the inheritance hierarchy and return a list of all the\nClass::MOP::Attribute objects for this class and its parents.\n\n$metaclass->findattributebyname($attributename)\nThis will return a Class::MOP::Attribute for the specified $attributename. If the class\ndoes not have the specified attribute, it returns \"undef\".\n\nUnlike \"getattribute\", this attribute *will* look for the named attribute in superclasses.\n\n$metaclass->addattribute(...)\nThis method accepts either an existing Class::MOP::Attribute object or parameters suitable\nfor passing to that class's \"new\" method.\n\nThe attribute provided will be added to the class.\n\nAny accessor methods defined by the attribute will be added to the class when the attribute\nis added.\n\nIf an attribute of the same name already exists, the old attribute will be removed first.\n\n$metaclass->removeattribute($attributename)\nThis will remove the named attribute from the class, and Class::MOP::Attribute object.\n\nRemoving an attribute also removes any accessor methods defined by the attribute.\n\nHowever, note that removing an attribute will only affect *future* object instances created\nfor this class, not existing instances.\n\n$metaclass->attributemetaclass\nReturns the class name of the attribute metaclass for this class. By default, this is\nClass::MOP::Attribute.\n"
                },
                {
                    "name": "Overload introspection and creation",
                    "content": "These methods provide an API to the core overload functionality.\n\n$metaclass->isoverloaded\nReturns true if overloading is enabled for this class. Corresponds to \"isoverloaded\" in\nDevel::OverloadInfo.\n\n$metaclass->getoverloadedoperator($op)\nReturns the Class::MOP::Overload object corresponding to the operator named $op, if one\nexists for this class.\n\n$metaclass->hasoverloadedoperator($op)\nReturns whether or not the operator $op is overloaded for this class.\n\n$metaclass->getoverloadlist\nReturns a list of operator names which have been overloaded (see \"Overloadable Operations\"\nin overload for the list of valid operator names).\n\n$metaclass->getalloverloadedoperators\nReturns a list of Class::MOP::Overload objects corresponding to the operators that have been\noverloaded.\n\n$metaclass->addoverloadedoperator($op, $impl)\nOverloads the operator $op for this class. The $impl can be a coderef, a method name, or a\nClass::MOP::Overload object. Corresponds to \"use overload $op => $impl;\"\n\n$metaclass->removeoverloadedoperator($op)\nRemove overloading for operator $op. Corresponds to \"no overload $op;\"\n\n$metaclass->getoverloadfallbackvalue\nReturns the overload \"fallback\" setting for the package.\n\n$metaclass->setoverloadfallbackvalue($fallback)\nSets the overload \"fallback\" setting for the package.\n"
                },
                {
                    "name": "Class Immutability",
                    "content": "Making a class immutable \"freezes\" the class definition. You can no longer call methods which\nalter the class, such as adding or removing methods or attributes.\n\nMaking a class immutable lets us optimize the class by inlining some methods, and also allows us\nto optimize some methods on the metaclass object itself.\n\nAfter immutabilization, the metaclass object will cache most informational methods that returns\ninformation about methods or attributes. Methods which would alter the class, such as\n\"addattribute\" and \"addmethod\", will throw an error on an immutable metaclass object.\n\nThe immutabilization system in Moose takes much greater advantage of the inlining features than\nClass::MOP itself does.\n\n$metaclass->makeimmutable(%options)\nThis method will create an immutable transformer and use it to make the class and its\nmetaclass object immutable, and returns true (you should not rely on the details of this\nvalue apart from its truth).\n\nThis method accepts the following options:\n\n*       inlineaccessors\n\n*       inlineconstructor\n\n*       inlinedestructor\n\nThese are all booleans indicating whether the specified method(s) should be inlined.\n\nBy default, accessors and the constructor are inlined, but not the destructor.\n\n*       immutabletrait\n\nThe name of a class which will be used as a parent class for the metaclass object\nbeing made immutable. This \"trait\" implements the post-immutability functionality of\nthe metaclass (but not the transformation itself).\n\nThis defaults to Class::MOP::Class::Immutable::Trait.\n\n*       constructorname\n\nThis is the constructor method name. This defaults to \"new\".\n\n*       constructorclass\n\nThe name of the method metaclass for constructors. It will be used to generate the\ninlined constructor. This defaults to \"Class::MOP::Method::Constructor\".\n\n*       replaceconstructor\n\nThis is a boolean indicating whether an existing constructor should be replaced when\ninlining a constructor. This defaults to false.\n\n*       destructorclass\n\nThe name of the method metaclass for destructors. It will be used to generate the\ninlined destructor. This defaults to \"Class::MOP::Method::Denstructor\".\n\n*       replacedestructor\n\nThis is a boolean indicating whether an existing destructor should be replaced when\ninlining a destructor. This defaults to false.\n\n$metaclass->immutableoptions\nReturns a hash of the options used when making the class immutable, including both defaults\nand anything supplied by the user in the call to \"$metaclass->makeimmutable\". This is\nuseful if you need to temporarily make a class mutable and then restore immutability as it\nwas before.\n\n$metaclass->makemutable\nCalling this method reverse the immutabilization transformation.\n"
                },
                {
                    "name": "Method Modifiers",
                    "content": "Method modifiers are hooks which allow a method to be wrapped with *before*, *after* and\n*around* method modifiers. Every time a method is called, its modifiers are also called.\n\nA class can modify its own methods, as well as methods defined in parent classes.\n\nHow method modifiers work?\nMethod modifiers work by wrapping the original method and then replacing it in the class's\nsymbol table. The wrappers will handle calling all the modifiers in the appropriate order and\npreserving the calling context for the original method.\n\nThe return values of \"before\" and \"after\" modifiers are ignored. This is because their purpose\nis not to filter the input and output of the primary method (this is done with an *around*\nmodifier).\n\nThis may seem like an odd restriction to some, but doing this allows for simple code to be added\nat the beginning or end of a method call without altering the function of the wrapped method or\nplacing any extra responsibility on the code of the modifier.\n\nOf course if you have more complex needs, you can use the \"around\" modifier which allows you to\nchange both the parameters passed to the wrapped method, as well as its return value.\n\nBefore and around modifiers are called in last-defined-first-called order, while after modifiers\nare called in first-defined-first-called order. So the call tree might looks something like\nthis:\n\nbefore 2\nbefore 1\naround 2\naround 1\nprimary\naround 1\naround 2\nafter 1\nafter 2\n\nWhat is the performance impact?\nOf course there is a performance cost associated with method modifiers, but we have made every\neffort to make that cost directly proportional to the number of modifier features you use.\n\nThe wrapping method does its best to only do as much work as it absolutely needs to. In order to\ndo this we have moved some of the performance costs to set-up time, where they are easier to\namortize.\n\nAll this said, our benchmarks have indicated the following:\n\nsimple wrapper with no modifiers             100% slower\nsimple wrapper with simple before modifier   400% slower\nsimple wrapper with simple after modifier    450% slower\nsimple wrapper with simple around modifier   500-550% slower\nsimple wrapper with all 3 modifiers          1100% slower\n\nThese numbers may seem daunting, but you must remember, every feature comes with some cost. To\nput things in perspective, just doing a simple \"AUTOLOAD\" which does nothing but extract the\nname of the method called and return it costs about 400% over a normal method call.\n\n$metaclass->addbeforemethodmodifier($methodname, $code)\nThis wraps the specified method with the supplied subroutine reference. The modifier will be\ncalled as a method itself, and will receive the same arguments as are passed to the method.\n\nWhen the modifier exits, the wrapped method will be called.\n\nThe return value of the modifier will be ignored.\n\n$metaclass->addaftermethodmodifier($methodname, $code)\nThis wraps the specified method with the supplied subroutine reference. The modifier will be\ncalled as a method itself, and will receive the same arguments as are passed to the method.\n\nWhen the wrapped methods exits, the modifier will be called.\n\nThe return value of the modifier will be ignored.\n\n$metaclass->addaroundmethodmodifier($methodname, $code)\nThis wraps the specified method with the supplied subroutine reference.\n\nThe first argument passed to the modifier will be a subroutine reference to the wrapped\nmethod. The second argument is the object, and after that come any arguments passed when the\nmethod is called.\n\nThe around modifier can choose to call the original method, as well as what arguments to\npass if it does so.\n\nThe return value of the modifier is what will be seen by the caller.\n"
                },
                {
                    "name": "Introspection",
                    "content": "Class::MOP::Class->meta\nThis will return a Class::MOP::Class instance for this class.\n\nIt should also be noted that Class::MOP will actually bootstrap this module by installing a\nnumber of attribute meta-objects into its metaclass.\n"
                }
            ]
        },
        "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": "Class::MOP::Class - Class Meta Object",
    "flags": [],
    "examples": [],
    "see_also": []
}