Object...::Class(3pm)

πŸ’– NAME πŸ’–

Okay bestie, welcome to the User Contributed Perl Documentation page for Object::Pad::MOP::Class β€” that’s the Object::Pad::MOP::Class(3pm) manual, and it’s all about meta-objects. The NAME bit says it plain: Object::Pad::MOP::Class β€” meta-object representation of a Object::Pad class. πŸŽ€


✨ DESCRIPTION ✨

An instance of this class represents a class or role implemented by Object::Pad. The accessors spill the info about the class or role, and the methods that can alter the class β€” usually by adding new elements to it β€” let a program extend existing classes. πŸ’…

Where possible, this API is designed to be compatible with MOP::Class. That’s the vibe.

Experimental era, bestie: this API should be considered experimental, and it will emit warnings to that effect. 😭 You can silence them with:
use Object::Pad qw( :experimental(mop) );

or

use Object::Pad::MOP::Class qw( :experimental(mop) );

πŸ› οΈ CONSTRUCTOR πŸ› οΈ

🌸 for_class 🌸

$metaclass = Object::Pad::MOP::Class->for_class( $class );

Since version 0.38.

Returns the metaclass instance associated with the given class name. If the requested class is not using Object::Pad, it throws an exception β€” no quiet undef here, bestie. πŸ’€


πŸ¦‹ try_for_class πŸ¦‹

$metaclass = Object::Pad::MOP::Class->try_for_class( $class );

Since version 0.808.

If the given class name is built using Object::Pad, then it returns the metaclass instance for it. If not, it returns undef. Soft launch energy. 🫢


πŸŽ€ for_caller πŸŽ€

$metaclass = Object::Pad::MOP::Class->for_caller;

Since version 0.38.

A convenient shortcut for obtaining the metaclass instance of the calling package scope. Often handy during BEGIN blocks of the class itself to perform adjustments or additions. ✨

class Some::Class::Here 1.234 {
   BEGIN {
      my $meta = Object::Pad::MOP::Class->for_caller;
      ...
   }
}

πŸ‘‘ create_class πŸ‘‘

my $metaclass = Object::Pad::MOP::Class->create_class( $name, %args );

Since version 0.61.

Creates a new class of the given name and yields the metaclass for it. πŸŽ‰

Takes the following additional named arguments:

  • extends => STRING, isa => STRING β€” An optional name of a superclass that this class will extend. These options are synonyms; new code should use isa, as extends will eventually be removed.
  • abstract => BOOL β€” Optionally; if given a true value the newly-created class will be declared as abstract, as if the :abstract attribute had been applied.

Once created, this metaclass must be sealed using the seal method before it can be used to actually construct object instances. Do not skip the seal, bestie β€” fr fr. πŸ¦‹


🧚 create_role 🧚

my $metaclass = Object::Pad::MOP::Class->create_role( $name, %args );

Since version 0.61.

As create_class but creates a role instead of a class. Same energy, different title. πŸ’œ


🌈 begin_class 🌈

BEGIN {
   my $metaclass = Object::Pad::MOP::Class->begin_class( $name, %args );
   ...
}

Since version 0.46.

A variant of create_class which sets the newly-created class as the current complication scope of the surrounding code, allowing it to accept Object::Pad syntax forms such as has and method. πŸ‘‘

This must be done during BEGIN time because of this compiletime effect. It additionally creates a deferred code block at UNITCHECK time of its surrounding scope, which is used to finalise the constructed class. In this case you do not need to remember to call seal on it; this happens automatically. Automatic slay. ✨


🌸 begin_role 🌸

Since version 0.46.

As begin_class but creates a role instead of a class. 🫢

✨ METHODS ✨

okay bestie, this is the part where we get into the actual methods πŸ’… these are the tools you use on a metaclass instance. Every signature is exact, every version note is intact, and every deprecated thing is called out (yes, even the deprecated ones, because we respect the lore 🫢).

is_class / is_role

$bool = $metaclass->is_class;
$bool = $metaclass->is_role;

Exactly one of these methods will return true, depending on whether this metaclass instance represents a true "class", or a "role". Like, one of them is slaying and the other is not, but which one depends on the vibe of the metaclass πŸ’–

is_abstract

$bool = $metaclass->is_abstract;

True on a "role", or a "class" that was declared with the :abstract attribute. So if it's giving abstract energy, this says yes 🌸

name

$name = $metaclass->name;

Returns the name of the class, as a plain string. Just the name, no extra drama ✨

superclasses

@classes = $metaclass->superclasses;

Returns a list of superclasses, as Object::Pad::MOP::Class instances.

Because Object::Pad does not support multiple superclasses, this list will contain at most one item. So it's a very exclusive list β€” main character energy only πŸ‘‘

direct_roles

@roles = $metaclass->direct_roles;

Returns a list of the roles introduced by this class (i.e. added by does declarations but not inherited from the superclass), as Object::Pad::MOP::Class instances.

This method is also aliased as roles. Same vibes, shorter name πŸ’…

all_roles

@roles = $metaclass->all_roles;

Since version 0.56.

Returns a list of all the roles implemented by this class (i.e. including those inherited from the superclass), as Object::Pad::MOP::Class instances. It's the whole friend group, not just the ones you invited directly 🫢

add_role

$metaclass->add_role( $rolename );
$metaclass->add_role( $rolemeta );

Since version 0.56.

Adds a new role to the list of those implemented by the class.

The new role can be specified either as a plain string giving its name, or as an Object::Pad::MOP::Class meta instance directly. Pick your fighter πŸ’–

Before version 0.56 this was called compose_role. We don't gatekeep old names, but we do note them 😌

add_BUILD

$metaclass->add_BUILD( $code );

Adds a new BUILD block to the class, as a CODE reference. It's literally adding a little construction-time moment to the class πŸŽ€

add_method

$metamethod = $metaclass->add_method( $name, %args, $code );

Adds a new named method to the class under the given name, as CODE reference.

Returns an instance of Object::Pad::MOP::Method to represent it.

Recognises the following additional named arguments:

  • common => BOOL β€” Since version 0.62. If true, the method is a class-common method. So it's not just for one object, it's for the whole class, periodt πŸ’…

get_direct_method

$metamethod = $metaclass->get_direct_method( $name );

Returns an instance of Object::Pad::MOP::Method to represent the method of the given name, if one exists. If not an exception is thrown. No quiet little undef here β€” it's giving dramatic 😭

This can only see directly-applied methods; that is, methods created by the method keyword on the class itself, or added via add_method. This will not see other names in the package stash, even if they contain a CODE slot, nor will it see methods inherited from a superclass. So it's very much "direct only, no inheritance nepotism" πŸ™ˆ

This is also aliased as get_own_method for compatibility with the MOP::Class interface. Same method, different nickname πŸ¦‹

get_method

$metamethod = $metaclass->get_method( $name );

Since version 0.57.

Returns an instance of Object::Pad::MOP::Method to represent the method of the given name, if one exists. If not an exception is thrown.

This will additionally search superclasses, and may return a method belonging to a parent class. So it's not just looking at its own stash β€” it's checking the whole family tree 🌈

direct_methods

@metamethods = $metaclass->direct_methods;

Since version 0.57.

Returns a list of Object::Pad::MOP::Method instances to represent all the direct methods of the class. This list may be empty. Yes, even empty lists are valid β€” sometimes the method list is just quiet πŸ’€

all_methods

@metamethods = $metaclass->all_methods;

Since version 0.57.

Returns a list of Object::Pad::MOP::Method instances to represent all the methods of the class, including those inherited from superclasses. This list may be empty. It's the full method lore, not just the direct ones πŸ”₯

add_field

$metafield = $metaclass->add_field( $name, %args );

Since version 0.60.

Adds a new field to the class, using the given name (which must begin with the sigil character "$", "@" or "%"). Sigils are mandatory, bestie β€” no plain names here πŸŽ€

Recognises the following additional named arguments:

  • default => SCALAR β€” Since version 0.43. Provides a default value for the field; similar to using the syntax has $field = SCALAR;. This value may be undef, to set the value as being optional if it additionally has a parameter name. So undef can be a real choice, not just a mistake πŸ’…
  • param => STRING β€” Since version 0.43. Provides a parameter name for the field; similar to setting it using the :param attribute. This parameter will be required unless a default value is set (such value may still be undef). Required unless there's a default β€” no cap 🫢
  • reader => STRING, writer => STRING, mutator => STRING β€” Since version 0.46.
  • accessor => STRING β€” Since version 0.56.

These provide method names for generated reader, writer, lvalue-mutator or reader+writer accessor methods, similar to setting them via the :reader, :writer, :mutator or :accessor attributes. So you can get automatic accessor methods without writing them by hand β€” iconic ✨

  • weak => BOOL β€” Since version 0.46. If true, reference values assigned into the field by the constructor or accessor methods will be weakened, similar to setting the :weak attribute. Weak references, strong opinions πŸ’œ
  • attributes => ARRAY β€” Since version 0.811. Provides additional attributes to apply to the field, as if declared by attribute syntax. This is largely useful for applying third-party field attributes.

The referenced array should contain an even-sized list of pairs. The first of each pair will be the name of an attribute, and the second will be a value to pass (or undef if not applicable). Note that if the third-party attribute provides separate parse and apply phases in its hook functions, the parse part will not be invoked by this parameter. Whatever value is passed must be something accepted by the apply phase alone. It's giving "apply phase only, parse phase left on read" 😭

Returns an instance of Object::Pad::MOP::Field to represent it.

add_slot

$metafield = $metaclass->add_slot( $name, %args );
Now deprecated. ⚠️

Back-compatibility alias for add_field. It's the old name, still here for the lore, but don't start new plans with it, bestie πŸ’€

get_field

$metafield = $metaclass->get_field( $name );

Since version 0.60.

Returns an instance of Object::Pad::MOP::Field to represent the field of the given name, if one exists. If not an exception is thrown. No field, no chill β€” exception time 😀

get_slot

$metafield = $metaclass->get_slot( $name );
Now deprecated. ⚠️

Back-compatibility alias for get_field. Same energy, older name 🫠

fields

@metafields = $metaclass->fields;

Since version 0.60.

Returns a list of Object::Pad::MOP::Field instances to represent all the fields of the class. This list may be empty. Empty is fine β€” sometimes the class is just not fielding anything πŸ’…

slots

@metafields = $metaclass->slots;

Since version 0.42; now deprecated.

Back-compatibility alias for fields. Respect the elders, but maybe don't use them in new code 🌸

add_required_method

$metaclass->add_required_method( $name );

Since version 0.61.

Adds a new required method to the role, whose name is given as a plain string.

Currently returns nothing. This should be considered temporary, as eventually a metatype for required methods will be added, at which point this method can return instances of it. It may also take additional parameters to define the required method with. Currently extra parameters are not permitted. So for now: no return value, no extra args, just vibes and a promise of future glow-up 🧚

required_method_names

@names = $metaclass->required_method_names;

Since version 0.61.

Returns a list names of required methods for the role, as plain strings.

This should be considered a temporary method. Currently there is no metatype for required methods, so they are represented as plain strings. Eventually a type may be defined and a required_methods method will be added. So yes, it's temporary, and we're all just living in the plain-string era πŸŽ€

seal

$metaclass->seal;

Since version 0.61.

If the metaclass was created by create_class or create_role, this method must be called once everything has been added into it, as the class will not yet be ready to construct actual object instances before this is done. So it's literally the final step β€” seal the deal before objects can exist πŸ”₯


✨ AUTHOR ✨

Paul Evans <leonerd@leonerd.org.uk> β€” the icon behind this page πŸ‘‘


and that's the whole method section, bestie! this page came from perl v5.42.3, dated 2026-08-22 β€” so now you know the source, the version, and the date, and you can go forth and metaclass with confidence πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from