Scroll to navigation

MakeMethods::Template::Universal(3pm) User Contributed Perl Documentation MakeMethods::Template::Universal(3pm)

NAME

Class::MakeMethods::Template::Universal - Meta-methods for any type of object

SYNOPSIS

  package MyObject;
  use Class::MakeMethods::Template::Universal (
    'no_op' => [ 'twiddle' ],
    'croak' => [ 'fail', { croak_msg => 'Curses!' } ]
  );
  
  package main;
  MyObject->twiddle;                    # Does nothing
  if ( $foiled ) { MyObject->fail() }   # Dies with croak_msg

DESCRIPTION

UNIVERSAL META-METHODS

The following meta-methods and behaviors are applicable across multiple types of classes and objects.

Universal:generic

This is not a directly-invokable method type, but instead provides code expressions for use in other method-generators.

You can use any of these features in your meta-method interfaces without explicitly importing them.

Modifiers

  • --private

    Causes the method to croak if it is called from outside of the package which originally declared it.

    Note that this protection can currently be circumvented if your class provides the method_init behavior, or another subroutine that calls methods by name.

  • --protected

    Causes the method to croak if it is called from a package other than the declaring package and its inheritors.

    Note that this protection can currently be circumvented if your class provides the method_init behavior, or another subroutine that calls methods by name.

  • --public

    Cancels any previous -private or -protected declaration.

  • --self_closure

    Causes the method to return a function reference which is bound to the arguments provided when it is first called.

    For examples of usage, see the test scripts in t/*closure.t.

  • --lvalue

    Adds the ":lvalue" attribute to the subroutine declaration.

    For examples of usage, see the test scripts in t/*lvalue.t.

  • --warn_calls

    For diagnostic purposes, call warn with the object reference, method name, and arguments before executing the body of the method.

Behaviors

  • attributes

    Runtime access to method parameters.

  • no_op -- See below.
  • croak -- See below.
  • method_init -- See below.

no_op

For each meta-method, creates a method with an empty body.

  use Class::MakeMethods::Template::Universal (
    'no_op' => [ 'foo bar baz' ],
  );

You might want to create and use such methods to provide hooks for subclass activity.

No interfaces or parameters supported.

croak

For each meta-method, creates a method which will croak if called.

  use Class::MakeMethods::Template::Universal (
    'croak' => [ 'foo bar baz' ],
  );

This is intended to support the use of abstract methods, that must be overridden in a useful subclass.

If each subclass is expected to provide an implementation of a given method, using this abstract method will replace the generic error message below with the clearer, more explicit error message that follows it:

  Can't locate object method "foo" via package "My::Subclass"
  The "foo" method is abstract and can not be called on My::Subclass

However, note that the existence of this method will be detected by UNIVERSAL::can(), so it is not suitable for use in optional interfaces, for which you may wish to be able to detect whether the method is supported or not.

The -unsupported and -prohibited interfaces provide alternate error messages, or a custom error message can be provided using the 'croak_msg' parameter.

method_init

Creates a method that accepts a hash of key-value pairs, or a reference to hash of such pairs. For each pair, the key is interpreted as the name of a method to call, and the value is the argument to be passed to that method.

Sample declaration and usage:

  package MyObject;
  use Class::MakeMethods::Template::Universal (
    method_init => 'init',
  );
  ...
  
  my $object = MyObject->new()
  $object->init( foo => 'Foozle', bar => 'Barbados' );
  
  # Equivalent to:
  $object->foo('Foozle');
  $object->bar('Barbados');

You might want to create and use such methods to allow easy initialization of multiple object or class parameters in a single call.

Note: including methods of this type will circumvent the protection of "private" and "protected" methods, because it an outside caller can cause an object to call specific methods on itself, bypassing the privacy protection.

forward_methods

Creates a method which delegates to an object provided by another method.

Example:

  use Class::MakeMethods::Template::Universal
    forward_methods => [ 
         --target=> 'whistle', w, 
        [ 'x', 'y' ], { target=> 'xylophone' }, 
        { name=>'z', target=>'zither', target_args=>[123], method_name=>do_zed },
      ];

Example: The above defines that method "w" will be handled by the calling "w" on the object returned by "whistle", whilst methods "x" and "y" will be handled by "xylophone", and method "z" will be handled by calling "do_zed" on the object returned by calling "zither(123)".

Interfaces:

Calls the method on the target object. If the target object is missing, croaks at runtime with a message saying "Can't forward bar because bar is empty."
Calls the method on the target object, if present. If the target object is missing, returns nothing.

Parameters: The following additional parameters are supported:

Required. The name of the method that will provide the object that will handle the operation.
Optional ref to an array of arguments to be passed to the target method.
The name of the method to call on the handling object. Defaults to the name of the meta-method being created.

SEE ALSO

See Class::MakeMethods for general information about this distribution.

See Class::MakeMethods::Template for information about this family of subclasses.

2022-10-13 perl v5.34.0