MakeMe...eneric(3pm)

๐Ÿ“ฆ
version 1.01 ยท 5 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

โœจ NAME โœจ

Class::MakeMethods::Template::Generic โ€” Templates for common meta-method types. This is the MakeMe...eneric(3pm) page, with User Contributed Perl Documentation doing its little header cameo too ๐Ÿ’…โœจ


๐ŸŒธ SYNOPSIS ๐ŸŒธ

package MyObject;
use Class::MakeMethods (
  'Template::Hash:new'       => [ 'new' ],
  'Template::Hash:scalar'    => [ 'foo' ]
  'Template::Static:scalar'  => [ 'bar' ]
);

package main;

my $obj = MyObject->new( foo => "Foozle", bar => "Bozzle" );
print $obj->foo();
$obj->bar("Bamboozle");

๐Ÿ’… DESCRIPTION ๐Ÿ’…

This package provides a variety of abstract interfaces for constructors and accessor methods, which form a common foundation for meta-methods provided by the Hash, Scalar, Flyweight, Static, PackageVar, and ClassVar implementations.

Generally speaking, the Generic meta-methods define calling interfaces and behaviors which are bound to differently scoped data by each of those subclasses. iykyk ๐Ÿฆ‹


๐Ÿฆ‹ new Constructor ๐Ÿฆ‹

There are several types of hash-based object constructors to choose from, bestie.

Each of these methods creates and returns a reference to a new blessed instance. They differ in how their (optional) arguments are interpreted to set initial values, and in how they operate when called as class or instance methods. main character constructor energy ๐Ÿ‘‘

๐Ÿ’– Interfaces

The following interfaces are supported.

  • -with_values โ€” Provides the with_values behavior.
  • -with_init โ€” Provides the with_init behavior.
  • -with_methods โ€” Provides the with_methods behavior.
  • -new_and_init โ€” Provides the with_init behavior for *, and the general purpose method_init behavior as an init method.
  • -copy_with_values โ€” Provides the copy behavior.

๐Ÿ”ฅ Behaviors

The following types of constructor methods are available.

  • with_values โ€” Creates and blesses a new instance.

If arguments are passed they are included in the instance, otherwise it will be empty.

Returns the new instance.

May be called as a class or instance method.

  • with_methods โ€” Creates, blesses, and returns a new instance.

The arguments are treated as a hash of method-name/argument-value pairs, with each such pair causing a call "$self->name($value)".

  • with_init โ€” Creates and blesses a new instance, then calls a method named "init", passing along any arguments that were initially given.

Returns the new instance.

The init() method should be defined in the class declaring these methods.

May be called as a class or instance method.

  • and_then_init โ€” Creates a new instance using method-name/argument-value pairs, like "with_methods", but then calls a method named "init" before returning the new object. The "init" method does not receive any arguments.

The init() method should be defined in the class declaring these methods.

  • instance_with_methods โ€” If called as a class method, creates, blesses, and returns a new instance. If called as an object method, operates on and returns the existing instance.

Accepts name-value pair arguments, or a reference to hash of such pairs, and calls the named method for each with the supplied value as a single argument. (See the Universal method_init behavior for more discussion of this pattern.)

  • copy_with values โ€” Produce a copy of an instance. Can not be called as a class method.

The copy is a shallow copy; any references will be shared by the instance upon which the method is called and the returned newborn.

If a list of key-value pairs is passed as arguments to the method, they are added to the copy, overwriting any values with the same key that may have been copied from the original.

  • copy_with_methods โ€” Produce a copy of an instance. Can not be called as a class method.

The copy is a shallow copy; any references will be shared by the instance upon which the method is called and the returned newborn.

Accepts name-value pair arguments, or a reference to hash of such pairs, and calls the named method on the copy for each with the supplied value as a single argument before the copy is returned.

  • copy_instance_with_values โ€” If called as a class method, creates, blesses, and returns a new instance. If called as an object method, produces and returns a copy of an instance.

The copy is a shallow copy; any references will be shared by the instance upon which the method is called and the returned newborn.

If a list of key-value pairs is passed as arguments to the method, they are added to the copy, overwriting any values with the same key that may have been copied from the original.

  • copy_instance_with_methods โ€” If called as a class method, creates, blesses, and returns a new instance. If called as an object method, produces and returns a copy of an instance.

The copy is a shallow copy; any references will be shared by the instance upon which the method is called and the returned newborn.

Accepts name-value pair arguments, or a reference to hash of such pairs, and calls the named method on the copy for each with the supplied value as a single argument before the copy is returned.

๐ŸŽ€ Parameters

The following parameters are supported:

  • init_method โ€” The name of the method to call after creating a new instance. Defaults to 'init'.

๐Ÿ’œ scalar Accessor ๐Ÿ’œ

A generic scalar-value accessor meta-method which serves as an abstraction for basic "get_set" methods and numerous related interfaces.

use Class::MakeMethods -MakerClass => "...",
      scalar => [ 'foo', 'bar' ];
...
$self->foo( 'my new foo value' );
print $self->foo();

(Note that while you can use the scalar methods to store references to various data structures, there are other meta-methods defined below that may be more useful for managing references to arrays, hashes, and objects.) no cap, that note matters ๐Ÿซถ

โœจ Interfaces

The following calling interfaces are available.

  • get_set (default) โ€” Provides get_set method for *.

Example: Create method foo, which sets the value of 'foo' for this instance if an argument is passed in, and then returns the value whether or not it's been changed:

use Class::MakeMethods -MakerClass => "...",
  scalar => [ 'foo' ];
  • get_protected_set โ€” Provides an get_set accessor for * that croaks if a new value is passed in from a package that is not a subclass of the declaring one.
  • get_private_set โ€” Provides an get_set accessor for * that croaks if a new value is passed in from a package other than the declaring one.
  • read_only โ€” Provides an accessor for * that does not modify its value. (Its initial value would have to be set by some other means.)
  • eiffel โ€” Provides get behavior as *, and set behavior as set_*.

Example: Create methods bar which returns the value of 'bar' for this instance (takes no arguments), and set_bar, which sets the value of 'bar' (no return):

use Class::MakeMethods -MakerClass => "...",
  scalar => [ --eiffel => 'bar' ];
  • java โ€” Provides get behavior as get*, and set behavior as set*.

Example: Create methods getBaz which returns the value of 'Baz' for this instance (takes no arguments), and setBaz, which sets the value for this instance (no return):

use Class::MakeMethods -MakerClass => "...",
  scalar => [ --java => 'Baz' ];
  • init_and_get โ€” Creates methods which cache their results in a hash key.

Provides the get_init behavior for *, and an delete behavior for clear_*. Specifies default value for init_method parameter of init_*.

  • with_clear โ€” Provides get_set behavior for *, and a clear_* method.

๐Ÿฆ‹ Behaviors

The following types of accessor methods are available.

  • get_set โ€” If no argument is provided, returns the value of the current instance. The value defaults to undef.

If an argument is provided, it is stored as the value of the current instance (even if the argument is undef), and that value is returned.

Also available as get_protected_set and get_private_set, which are available for public read-only access, but have access control limitations.

  • get โ€” Returns the value from the current instance.
  • set โ€” Sets the value for the current instance. If called with no arguments, the value is set to undef. Does not return a value.
  • clear โ€” Sets value to undef.
  • get_set_chain โ€” Like get_set, but if called with an argument, returns the object it was called on. This allows a series of mutators to be called as follows:
package MyObject;
use Class::MakeMethods (
  'Template::Hash:scalar --get_set_chain' => 'foo bar baz'
);
...

my $obj = MyObject->new->foo('Foozle');
$obj->bar("none")->baz("Brazil");
print $obj->foo, $obj->bar, $obj->baz;
  • get_set_prev โ€” Like get_set, but if called with an argument, returns the previous value before it was changed to the new one.
  • get_init โ€” If the value is currently undefined, calls the init_method. Returns the value.

๐ŸŽ€ Parameters

The following parameters are supported:

  • init_method โ€” The name of a method to be called to initialize this meta-method.

Only used by the get_init behavior.


๐ŸŒˆ string Accessor ๐ŸŒˆ

A generic scalar-value accessor meta-method which serves as an abstraction for basic "get_set" methods and numerous related interfaces.

use Class::MakeMethods -MakerClass => "...",
      string => [ 'foo', 'bar' ];
...
$self->foo( 'my new foo value' );
print $self->foo();

This meta-method extends the scalar meta-method, and supports the same interfaces and parameters.

However, it generally treats values as strings, and can not be used to store references. string behavior only, bestie ๐Ÿ“

โญ Interfaces

In addition to those provided by "scalar", the following calling interfaces are available.

  • -get_concat โ€” Provides the get_concat behavior for *, and a clear_* method.

Example:

use Class::MakeMethods
  get_concat => { name => 'words', join => ", " };

$obj->words('foo');
$obj->words('bar');
$obj->words() eq 'foo, bar';

๐Ÿ”ฅ Behaviors

In addition to those provided by "scalar", the following types of accessor methods are available.

  • concat โ€” Concatenates the argument value with the existing value.
  • get_concat โ€” Like get_set except sets do not clear out the original value, but instead concatenate the new value to the existing one.

๐Ÿ’œ Parameters

In addition to those provided by "scalar", the following parameters are supported.

๐ŸŽ€ join

If the join parameter is defined, every time the get_concat behavior gets invoked, it glues its argument onto any existing value using the join string as the separator. The join field is applied between values, not prior to the first or after the last. Defaults to undefined. ๐Ÿซถ

๐ŸŒธ string_index

string_index => [ qw / foo bar baz / ]

Creates string accessor methods, like string above, but also maintains a static hash index where each object is stored under the value of the field when the slot is set. โœจ

This is a unique index, so only one object can have a given key. If an object has a slot set to a value that another object is already set to, the object currently set to that value has that slot set to undef, and the new object gets put into the hash under that value. No cap.

Objects with undefined values are not stored in the index.

Note that to free items from memory, you must clear these values! ๐Ÿ˜ญ

Methods:

  • The method find_x is defined; if called with any arguments, it returns a list of the objects stored under those values in the hash. Called with no arguments, it returns a reference to the hash.

Profiles:

  • find_or_new
'string_index -find_or_new' => [ qw / foo bar baz / ]

Just like string_index, except the find_x method is defined to call the new method to create an object if there is no object already stored under any of the keys you give as arguments. ๐Ÿ’…

๐Ÿ”ฅ number Accessor

A generic scalar-value accessor meta-method that serves as an abstraction for basic get_set methods and numerous related interfaces. ๐Ÿงš

use Class::MakeMethods -MakerClass => "...",
      string => [ 'foo', 'bar' ];
...
$self->foo( 23 );
print $self->foo();

This meta-method extends the scalar meta-method, and supports the same interfaces and parameters.

However, it generally treats values as numbers, and can not be used to store strings or references. โš ๏ธ

Interfaces: In addition to those provided by scalar, the following calling interfaces are available.

  • -counter โ€” provides the numeric get_set behavior for *, and numeric *_incr and *_reset methods.

Behaviors: In addition to those provided by scalar, the following types of accessor methods are available.

  • get_set โ€” the get_set behavior is similar to the default scalar behavior except that empty values are treated as zero.
  • increment โ€” if no argument is provided, increments the hash_key value by 1. If an argument is provided, the value is incremented by that amount. Returns the increased value.
  • clear โ€” sets the value to zero.

๐Ÿฆ‹ boolean Accessor

A generic scalar-value accessor meta-method that serves as an abstraction for basic get_set methods and numerous related interfaces. ๐Ÿ’–

use Class::MakeMethods -MakerClass => "...",
      string => [ 'foo', 'bar' ];
...
$self->foo( 1 );
print $self->foo();
$self->clear_foo;

This meta-method extends the scalar meta-method, and supports the same interfaces and parameters. However, it generally treats values as true-or-false flags, and can not be used to store strings, numbers, or references. ๐Ÿ™ˆ

Interfaces:

  • flag_set_clear (default) โ€” provides the get_set behavior for *, and set_* and clear_* methods to set the value to true or false.

Behaviors: In addition to those provided by scalar, the following types of accessor methods are available.

  • get_set โ€” the get_set behavior is similar to the get_set scalar behavior except that empty or false values are treated as zero, and true values are treated as zero.
  • set_true โ€” sets the value to one.
  • set_false โ€” sets the value to zero.

๐ŸŒˆ bits Accessor

A generic accessor for bit-field values.

The difference between 'Template::Generic:bits' and 'Template::Generic:boolean' is that all flags created with this meta-method are stored in a single vector for space efficiency. โšก

Interfaces: The following calling interfaces are available.

  • default โ€” provides get_set behavior for *, a set_* method which sets the value to true, and a clear_* method which sets the value to false.

Also defines methods named bits, bit_fields, and bit_dump with the behaviors below. These methods are shared across all of the boolean meta-methods defined by a single class.

  • class_methods โ€” .

Basic Behaviors: The following types of bit-level accessor methods are available.

  • get_set โ€” returns the value of the named flag. If called with an argument, it first sets the named flag to the truth-value of the argument.
  • set_true โ€” sets the value to true.
  • set_false โ€” sets the value to false.

Group Methods: The following types of methods manipulate the overall vector value. ๐Ÿ‘‘

  • bits โ€” returns the vector containing all of the bit fields (remember, however, that a vector containing all 0 bits is still true).
  • bit_dump โ€” returns a hash of the flag-name/flag-value pairs.
  • bits_size โ€” returns the number of bits that can fit into the current vector.
  • bits_complement โ€” returns the twos-complement of the vector.
  • bit_pos_get โ€” takes a single argument and returns the value of the bit stored in that position.
  • bit_pos_set โ€” takes two arguments and sets the bit stored in the position of the first argument to the value of the second argument.

Class Methods: The following types of class methods are available.

  • bit_names โ€” returns a list of all the flags by name.

๐Ÿ“ array Accessor

Creates accessor methods for manipulating arrays of values. โœจ

Interfaces: The following calling interfaces are available.

  • default โ€” provides get_set behavior for *, and verb_* methods for the non-get behaviors below.
  • minimal โ€” provides get_set behavior for *, and *_verb methods for clear behavior.
  • get_set_items โ€” provides the get_set_items for *.
  • x_verb โ€” provides get_push behavior for *, and *_verb methods for the non-get behaviors below.
  • get_set_ref โ€” provides the get_set_ref for *.
  • get_set_ref_help โ€” provides the get_set_ref for *, and verb_* methods for the non-get behaviors below.

Behaviors: The following types of accessor methods are available.

  • get_set_items โ€” called with no arguments, returns a reference to the array stored in the slot.

Called with one simple scalar argument, it treats the argument as an index and returns the value stored under that index.

Called with more than one argument, it treats them as a series of index/value pairs and adds them to the array.

  • get_push โ€” if arguments are passed, these values are pushed on to the list; if a single array ref is passed, its values are used as the arguments.

This method returns the list of values stored in the slot. In an array context it returns them as an array, and in a scalar context as a reference to the array.

  • get_set_ref โ€” if arguments are passed, these values are placed on the list, replacing the current contents; if a single array ref is passed, its values are used as the arguments.

This method returns the list of values stored in the slot. In an array context it returns them as an array, and in a scalar context as a reference to the array.

  • get_set โ€” if arguments are passed, these values are placed on the list, replacing the current contents.

This method returns the list of values stored in the slot. In an array context it returns them as an array, and in a scalar context as a reference to the array.

  • push โ€” append items to tail.
  • pop โ€” remove an item from the tail.
  • shift โ€” remove an item from the front.
  • unshift โ€” prepend items to front.
  • splice โ€” remove or replace items.
  • clear โ€” remove all items.
  • count โ€” returns the number of item in the list.

๐Ÿ’œ hash Accessor

Creates accessor methods for manipulating hashes of key-value pairs. ๐ŸŽ€

Interfaces: The following calling interfaces are available.

  • default โ€” provides get_set behavior for *, and *_verb methods for most of the other behaviors below.
  • get_set_items โ€” provides the get_set_items for *.

Behaviors: The following types of accessor methods are available.

  • get_set_items โ€” called with no arguments, returns a reference to the hash stored.

Called with one simple scalar argument, it treats the argument as a key and returns the value stored under that key.

Called with more than one argument, it treats them as a series of key/value pairs and adds them to the hash.

  • get_push โ€” called with no arguments, returns the hash stored, as a hash in a list context or as a reference in a scalar context.

Called with one simple scalar argument, it treats the argument as a key and returns the value stored under that key.

Called with one array reference argument, the array elements are considered to be be keys of the hash. x returns the list of values stored under those keys (also known as a hash slice.)

Called with one hash reference argument, the keys and values of the hash are added to the hash.

Called with more than one argument, it treats them as a series of key/value pairs and adds them to the hash.

  • get_set โ€” like get_push, except if called with more then one argument, empties the current hash items before adding those arguments to the hash.
  • push โ€” called with one hash reference argument, the keys and values of the hash are added to the hash.

Called with more than one argument, it treats them as a series of key/value pairs and adds them to the hash.

  • keys โ€” returns a list of the keys of the hash.
  • values โ€” returns a list of the values in the hash.
  • tally โ€” takes a list of arguments and for each scalar in the list increments the value stored in the hash and returns a list of the current (after the increment) values.
  • exists โ€” takes a single key, returns whether that key exists in the hash.
  • delete โ€” takes a list, deletes each key from the hash, and returns the corresponding values.
  • clear โ€” resets hash to empty.

๐ŸŒŸ tiedhash Accessor

A variant of Generic:hash which initializes the hash by tieing it to a caller-specified package. ๐Ÿซถ

See the documentation on "Generic:hash" for interfaces and behaviors.

Parameters: The following parameters must be provided:

  • tie โ€” Required. The name of the class to tie to. Make sure you have "use"d the required class.
  • args โ€” Required. Any extra arguments for the tie, and you pass them as an array ref ๐ŸŽ€

Like, fr fr, here's what it looks like in the wild:

use Class::MakeMethods
  tie_hash => [ hits => { tie => q/Tie::RefHash/, args => [] } ];

use Class::MakeMethods
  tie_hash => [ [qw(hits errors)] => { tie => q/Tie::RefHash/, args => [] } ];

use Class::MakeMethods
  tie_hash => [ { name => hits, tie => q/Tie::RefHash/, args => [] } ];

Three vibes, same energy ๐Ÿ’…


๐Ÿฆ‹ hash_of_arrays Accessor

Makes accessor methods for messing with hashes of array-refs. Bestie, it's a hash where every value is a little list, and these methods are your emotional support tools for getting at them ๐ŸŒธ

Interfaces

These are the calling interfaces you can choose from:

  • default โ€” gives you get behavior for *, plus *_verb methods for all the other behaviors listed down there ๐Ÿ‘‡

Behaviors

Here's the full menu of accessor methods:

  • get โ€” returns all the values for all the keys you hand it, in order. If you pass no keys at all, it returns all the values (in an unspecified key order โ€” so don't @ me about ordering ๐Ÿ˜ญ).

The result comes back as an arrayref in scalar context. Important: this arrayref is not part of the data structure, so messing with it will not affect the contents directly (even if you only passed a single key as the argument). It's giving detached copy energy โœจ

If any argument is an arrayref, then the members of that array are used as the keys. So the trivial empty-key case is available with an argument of [] โ€” iconic.

  • keys โ€” returns the keys of the hash. As an arrayref in scalar context. ๐Ÿ—๏ธ
  • exists โ€” takes a list of keys and returns whether all of them exist in the hash (i.e., the "and" of whether the individual keys exist). All-or-nothing, main character behavior only ๐Ÿ‘‘
  • delete โ€” takes a list, and deletes each key from the hash. Gone. Periodt ๐Ÿ’€
  • push โ€” takes a key, and some values, and pushes those values onto the list denoted by the key. If the first argument is an arrayref, then each element of that arrayref is treated as a key, and the elements get pushed onto each appropriate list. Multipurpose slay ๐Ÿซถ
  • pop โ€” takes a list of keys and pops each one. Returns the list of popped elements. undef shows up in the list for each key that has an empty list. ngl, empty lists are so real for that.
  • unshift โ€” like push, only from the other end of the lists ๐Ÿ”„
  • shift โ€” like pop, only from the other end of the lists ๐Ÿ”„
  • splice โ€” takes a key, offset, length, and a values list, and splices the list named by the key. Anything from the offset argument (inclusive) may be omitted. See "splice" in perlfunc if you want the full lore ๐Ÿ“–
  • clear โ€” takes a list of keys and resets each named list to empty (but does not delete the keys). Clean slate, keys stay ๐Ÿค
  • count โ€” takes a list of keys and returns the sum of the number of elements for each named list. Math but make it cute ๐Ÿงฎ
  • index โ€” takes a key and a list of indices, and returns a list of each item at the corresponding index in the list of the given key. Uses undef for indices beyond range, no cap.
  • remove โ€” takes a key and a list of indices, and removes each corresponding item from the named list. The indices are effectively looked up at the point of call โ€” so removing indices 3, 1 from list (a, b, c, d) will remove (d) and (b). Sneaky but correct ๐Ÿ˜Œ
  • sift โ€” takes a key and a set of named arguments (which may be a list or a hash ref), and removes list members based on a grep-like approach. Options:
    • filter โ€” the filter function used (as a coderef). It's passed two arguments: the value compared against, and the value in the list that's a potential grepping-out candidate. If it returns true, the value is removed. Default is sub { $_[0] == $_[1] } ๐Ÿงš
    • keys โ€” the list keys to sift through (as an arrayref). Unknown keys are ignored. Default: all the known keys.
    • values โ€” the values to sift out (as an arrayref). Default: [undef]

๐Ÿ’œ object Accessor

Creates accessor methods for manipulating references to objects. We love a classy reference ๐Ÿ’…

On top of creating a method to get and set the object reference, the meta-method can also define forwarded methods that automatically pass calls onto the object stored in that slot โ€” see the description of the 'delegate' parameter below. iykyk ๐Ÿ”ฅ

Interfaces

  • default โ€” provides get_set behavior for *, clear behavior for 'delete_*', and forwarding methods for any values in the method's 'delegate' or 'soft_delegate' parameters.
  • get_and_set โ€” provides a named get method, plus set_x and clear_x methods.
  • get_init_and_set โ€” provides a named get_init method, plus set_x and clear_x methods.

Behaviors

  • get_set โ€” if called with a reference to an object of the given class as the first argument, it stores it ๐Ÿซณโœจ

If called with any other arguments, it creates and stores a new object, passing the arguments to the new() method for the object.

If called without arguments, it returns the current value, which may be undefined if one has not been stored yet. Mood.

  • get_set_init โ€” if called with a reference to an object of the given class as the first argument, it stores it.

If the slot is not filled yet, it creates an object by calling the given new method of the given class. Any arguments passed to the get_set_init method are passed on to new.

In all cases, the object now stored is returned. Always returns the goods ๐Ÿ’–

  • get_init โ€” if the instance is empty, it creates and stores a new one. Returns the instance.
  • get โ€” returns the current value, which may be undefined if one has not been stored yet.
  • set โ€” if called with a reference to an object of the given class as the first argument, it stores it.

If called with any other arguments, it creates and stores a new object, passing the arguments to the new() method.

If called without arguments, it creates and stores a new object, without any arguments to the new() method. Zero-arg new, no notes ๐Ÿ™ˆ

  • clear โ€” removes the reference value. Bye bestie ๐Ÿ‘‹
  • forwarding โ€” if a 'delegate' or 'soft_delegate' parameter is provided, methods with those names are created that are forwarded directly to the object in the slot, as described below.

Parameters

  • class โ€” Required. The type of object that will be stored. You gotta know the class, babe.
  • new_method โ€” the name of the method to call on the above class to create a new instance. Defaults to 'new'.
  • delegate โ€” the methods to forward to the object. Can contain a method name, a string of space-separated method names, or an array of method names. This type of method will croak if it is called when the target object is not defined ๐Ÿ˜ค (fair, honestly)
  • soft_delegate โ€” the methods to forward to the object, if it is present. Can contain a method name, a string of space-separated method names, or an array of method names. This type of method will return nothing if it is called when the target object is not defined. Soft launch energy ๐Ÿ’

๐ŸŽ€ instance Accessor

Creates methods to handle an instance of the calling class. Very "be your own best friend" of it ๐ŸŒˆ

PROFILES

  • default โ€” provides a named get method, and verb_x set, new, and clear methods.
  • -implicit_new โ€” provides a named get_init method, and verb_x set, and clear methods.
  • -x_verb โ€” provides a named get method, and x_verb set, new, and clear methods.

Behaviors

  • get โ€” returns the value of the instance parameter, which may be undefined if one has not been stored yet.
  • get_init โ€” if the instance is empty, it creates and stores a new one. Returns the instance.
  • set โ€” takes a single argument and sets the instance to that value. One argument only, king ๐Ÿ’…
  • new โ€” creates and stores a new instance. Fresh out the box โœจ
  • clear โ€” sets the instance parameter to undef.

Parameters

  • instance โ€” holds the instance reference. Defaults to undef
  • new_method โ€” the name of the method to call when creating a new instance. Defaults to 'new'.

๐Ÿ“ array_of_objects Accessor

Creates accessor methods for manipulating references to arrays of object references. It's a whole friend group in an array, tbh ๐Ÿซถ

Operates like "Generic:array", but before adding any item to the array, it first checks to see if it's an instance of the designated class โ€” and if not, it passes it as an argument to that class's new method and stores the result instead. Gatekeeping, but make it helpful ๐Ÿ‘‘

Forwarded methods return a list of the results returned by maping the method over each object in the array. Group chat broadcast behavior, love that.

See the documentation on "Generic:array" for interfaces and behaviors.

Parameters

  • class โ€” Required. The type of object that will be stored.
  • delegate โ€” the methods to forward to the object. Can contain a method name, a string of space-separated method names, or an array of method names.
  • new_method โ€” the name of the method to call on the above class to create a new instance. Defaults to 'new'.

โญ code Accessor

Creates accessor methods for manipulating references to subroutines. Function refs but make it fashion ๐Ÿ’…

Interfaces

  • default โ€” provides the call_set functionality.
  • method โ€” provides the call_method functionality.

Behaviors

  • call_set โ€” if called with one argument which is a CODE reference, it installs that code in the slot. Otherwise, it runs the code stored in the slot with whatever arguments (including none) were passed in. No args? Still runs. Iconic ๐Ÿƒโ€โ™€๏ธ
  • call_method โ€” just like call_set, except the code is called like a method, with $self as its first argument. Basically, you are creating a method which can be different for each object. Per-object personality, we stan ๐Ÿ˜ญ

๐Ÿงš code_or_scalar Accessor

Creates accessor methods for manipulating either strings or references to subroutines. Both? Both. ๐Ÿ’–

You can store any scalar value; code refs are executed when you retrieve the value, while other scalars are returned as-is.

Interfaces

  • default โ€” provides the call_set functionality.
  • method โ€” provides the call_method functionality.
  • eiffel โ€” provides the named get_method, and a helper set_* method.

Behaviors

  • get_set_call โ€” if called with an argument, either a CODE reference or some other scalar, it installs that code in the slot. Otherwise, it runs the code stored in the slot with whatever arguments (including none) were passed in.
  • get_set_method โ€” just like call_set, except the code is called like a method, with $self as its first argument. Basically, you are creating a method which can be different for each object. Same energy, method vibes โœจ

โœจ SEE ALSO โœจ

Okay so if you wanna go deeper (and bestie, you know you do ๐Ÿ’…), there's more reading waiting for you:

  • Class::MakeMethods โ€” this is the mothership ๐Ÿ›ธ, the place to go for the general tea about how this whole distribution works.
  • Class::MakeMethods::Template โ€” and this one's all about this little family of subclasses right here, so if the templating life is your vibe, this is your moment. ๐Ÿฆ‹

That's genuinely it, no cap โ€” those two are the doors to walk through next.


And that's a wrap on the whole page, bestie! ๐ŸŽ€ This little beauty comes to you courtesy of perl v5.36.0 and it was dated 2022-10-13 โ€” iconic behavior from the perl docs team, honestly ๐Ÿ‘‘. Go forth and templatize responsibly ๐Ÿ’–โœจ

๐Ÿ“ฆ where this page comes from