Class::...nstance(3)
β¨ NAME β¨
Class::MOP::Instance β the Instance Meta Object, aka the lil guy behind the scenes making your objects exist π (this page is from the User Contributed Perl Documentation, bestie, and the manual name is literally Class::...nstance(3) β yes that's how it's written, no I did not typo it, the header just does that)
πΈ VERSION πΈ
version 2.4000 β that's the one, periodt.
π¦ DESCRIPTION π¦
Okay so. The Instance Protocol is the whole vibe that controls two things:
- the creation of object instances π
- the storage of attribute values in those instances π
β οΈ Real talk: using this API directly in your own code violates encapsulation. Not a cute look. We recommend you use the appropriate APIs inClass::MOP::ClassandClass::MOP::Attributeinstead β those APIs in turn call the methods in this class as appropriate. So like, let the professionals handle it, iykyk.
This class also participates in generating inlined code by providing snippets of code to access an object instance. Yes, it's giving compiler-adjacent. Obsessed. π₯
π METHODS π
π Object construction
Class::MOP::Instance->new(%options)
This method creates a new meta-instance object. Fresh out the box, no cap.
It accepts the following keys in %options:
associated_metaclassβ theClass::MOP::Classobject for which instances will be created. It's the parent of the whole situation fr fr.attributesβ an array reference ofClass::MOP::Attributeobjects. These are the attributes which can be stored in each instance. Basically the inventory of what each instance is allowed to hold π
π Creating and altering instances
$metainstance->create_instanceβ returns a reference blessed into the associated metaclass's class. The default is to use a hash reference. Subclasses can override this, main character energy π«Ά$metainstance->clone_instance($instance)β given an instance, this method creates a new object by making a shallow clone of the original. Shallow, bestie, not deep. Don't come for me.
π§ Introspection
$metainstance->associated_metaclassβ returns theClass::MOP::Classobject associated with the meta-instance object.$metainstance->get_all_slotsβ returns a list of slot names stored in object instances. In almost all cases, slot names correspond directly attribute names. (Yes the original says it exactly like that, I'm not editing the lore β¨)$metainstance->is_valid_slot($slot_name)β returns true if$slot_nameis a valid slot name. Simple as. π$metainstance->get_all_attributesβ returns a list of attributes corresponding to the attributes passed to the constructor.
π Operations on Instance Structures
It's important to understand that the meta-instance object is a different entity from the actual instances it creates. Like, two totally separate beings. For this reason, any operations on the $instance_structure always require that the object instance be passed to the method. No shortcuts, no exceptions. π
$metainstance->get_slot_value($instance_structure, $slot_name)
$metainstance->set_slot_value($instance_structure, $slot_name, $value)
$metainstance->initialize_slot($instance_structure, $slot_name)
$metainstance->deinitialize_slot($instance_structure, $slot_name)
$metainstance->initialize_all_slots($instance_structure)
$metainstance->deinitialize_all_slots($instance_structure)
$metainstance->is_slot_initialized($instance_structure, $slot_name)
$metainstance->weaken_slot_value($instance_structure, $slot_name)
$metainstance->slot_value_is_weak($instance_structure, $slot_name)
$metainstance->strengthen_slot_value($instance_structure, $slot_name)
$metainstance->rebless_instance_structure($instance_structure, $new_metaclass)
The exact details of what each method does should be fairly obvious from the method name. Genuinely. Read it out loud. Slay. π
β Inlinable Instance Operations
$metainstance->is_inlinableβ this is a boolean that indicates whether or not slot access operations can be inlined. By default it is true, but subclasses can override this.$metainstance->inline_create_instance($class_variable)β this method expects a string that, when inlined, will become a class name. This would literally be something like'$class', not an actual class name. It returns a snippet of code that creates a new object for the class. This is something like" bless {}, $class_name ".$metainstance->inline_get_is_lvalueβ returns whether or not"inline_get_slot_value"is a valid lvalue. This can be used to do extra optimizations when generating inlined methods. Optimization queen π
$metainstance->inline_slot_access($instance_variable, $slot_name)
$metainstance->inline_get_slot_value($instance_variable, $slot_name)
$metainstance->inline_set_slot_value($instance_variable, $slot_name, $value)
$metainstance->inline_initialize_slot($instance_variable, $slot_name)
$metainstance->inline_deinitialize_slot($instance_variable, $slot_name)
$metainstance->inline_is_slot_initialized($instance_variable, $slot_name)
$metainstance->inline_weaken_slot_value($instance_variable, $slot_name)
$metainstance->inline_strengthen_slot_value($instance_variable, $slot_name)
These methods all expect two arguments. The first is the name of a variable that, when inlined, will represent the object instance. Typically this will be a literal string like '$_[0]'. The second argument is a slot name. The method returns a snippet of code that, when inlined, performs some operation on the instance. Code generation but make it fashion π
$metainstance->inline_rebless_instance_structure($instance_variable, $class_variable)β this takes the name of a variable that will, when inlined, represent the object instance, and the name of a variable that will represent the class to rebless into, and returns code to rebless an instance into a class. Glow-up for your object, literally π₯
πΈ Introspection
Class::MOP::Instance->meta
This will return a Class::MOP::Class instance for this class.
It should also be noted that Class::MOP will actually bootstrap this module by installing a number of attribute meta-objects into its metaclass. It's meta all the way down, bestie, and honestly? Iconic. π
π«Ά AUTHORS π«Ά
- Stevan Little <stevan@cpan.org>
- Dave Rolsky <autarch@urth.org>
- Jesse Luehrs <doy@cpan.org>
- Shawn M Moore <sartak@cpan.org>
- ΧΧΧΧ Χ§ΧΧ'ΧΧ (Yuval Kogman) <nothingmuch@woobling.org>
- Karen Etheridge <ether@cpan.org>
- Florian Ragwitz <rafl@debian.org>
- Hans Dieter Pearcey <hdp@cpan.org>
- Chris Prather <chris@prather.org>
- Matt S Trout <mstrout@cpan.org>
A whole squad. A whole ensemble cast. We love a collaborative effort β¨
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2006 by Infinity Interactive, Inc.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. Free as in freedom, free as in slay ποΈ
And that's the whole page, babe β this was perl v5.42.0, dated 2025-07-25, and the manual name on the tin is Class::...nstance(3). Mwah, take care of your instances π
π
π¦ where this page comes from
- fedora 43: perl-Moose 2.4000-4.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms