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 in Class::MOP::Class and Class::MOP::Attribute instead β€” 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 β€” the Class::MOP::Class object for which instances will be created. It's the parent of the whole situation fr fr.
  • attributes β€” an array reference of Class::MOP::Attribute objects. 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 the Class::MOP::Class object 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_name is 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 ✨


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