The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Object::Pad::MOP::Class - meta-object representation of a Object::Pad class

DESCRIPTION

Instances of this class represent a class or role implemented by Object::Pad. Accessors provide information about the class or role, and methods that can alter the class, typically by adding new elements to it, allow a program to extend existing classes.

Where possible, this API is designed to be compatible with MOP::Class.

This API should be considered experimental even within the overall context in which Object::Pad is expermental.

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.

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;
         ...
      }
   }

begin_class

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

Since version 0.46.

Creates a new class of the given name and yields the metaclass for it. This must be done during BEGIN time, as it creates a deferred code block at UNITCHECK time of its surrounding scope, which is used to finalise the constructed class.

Takes the following additional named arguments:

extends => STRING

An optional name of a superclass that this class will extend.

begin_role

Since version 0.46.

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

METHODS

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.

name

   $name = $metaclass->name

Returns the name of the class, as a plain string.

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.

roles

   @roles = $metaclass->roles

Returns a list of roles implemented by this class, as Object::Pad::MOP::Class instances.

compose_role

   $metaclass->compose_role( $rolename )
   $metaclass->compose_role( $rolemeta )

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.

add_BUILD

   $metaclass->add_BUILD( $code )

Adds a new BUILD block to the class, as a CODE reference.

add_method

   $metamethod = $metaclass->add_method( $name, $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.

get_own_method

   $metamethod = $metaclass->get_own_method( $name )

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

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.

add_slot

   $metaslot = $metaclass->add_slot( $name, %args )

Adds a new slot to the class, using the given name (which must begin with the sigil character $, @ or %).

Recognises the following additional named arguments:

default => SCALAR

Since version 0.43.

Provides a default value for the slot; similar to using the syntax

   has $slot = SCALAR;

This value may be undef, to set the value as being optional if it additionally has a parameter name.

param => STRING

Since version 0.43.

Provides a parameter name for the slot; 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).

reader => STRING
writer => STRING
mutator => STRING

Since version 0.46.

Provides method names for generated reader, writer or lvalue-mutator accessor methods, similar to setting them via the :reader, :writer or :mutator attributes.

weak => BOOL

Since version 0.46.

If true, reference values assigned into the slot by the constructor or accessor methods will be weakened, similar to setting the :weak attribute.

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

get_slot

   $metaslot = $metaclass->get_slot( $name )

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

slots

   @metaslots = $metaclass->slots

Since version 0.42.

Returns a list of Object::Pad::MOP::Slot instances to represent all the slots of the class. This list may be empty.

AUTHOR

Paul Evans <leonerd@leonerd.org.uk>