MixinFactory(3pm)

πŸ“¦
version 0.92 Β· 3 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– MixinFactory β€” Class::MixinFactory(3pm) πŸ’–

okay bestie, sit down, hydrate, and let me tell you about Class::MixinFactory ✨ this is a User Contributed Perl Documentation page, which is basically the group chat of Perl docs β€” unfiltered, beloved, iconic. 🫢


🌸 NAME 🌸

Class::MixinFactory β€” Class Factory with Selection of Mixins πŸ’…


✨ SYNOPSIS ✨

no cap, here's the whole vibe in one code block, look at her go:

package MyClass;
use Class::MixinFactory -hasafactory;
sub new { ... }
sub foo { return "Foo Bar" }

package MyClass::Logging;
sub foo { warn "Calling foo"; (shift)->NEXT('foo', @_) }

package MyClass::UpperCase;
sub foo { uc( (shift)->NEXT('foo', @_) ) }

package main;

my $class = MyClass->class( 'Logging', 'UpperCase' );
print $class->new()->foo();
# Calls MyClass::Logging::foo, MyClass::UpperCase::foo, MyClass::foo

and the order literally matters, notice that the calls go MyClass::Logging::foo, then MyClass::UpperCase::foo, then MyClass::foo. πŸ’€


πŸ¦‹ DESCRIPTION πŸ¦‹

This distribution does the most: it facilitates the run-time generation of classes which inherit from a base class and some optional selection of mixin classes. it's giving factory-core. 🏭

A factory is provided to generate the mixed classes with multiple inheritance. and there's a NEXT method that lets method redispatch up the inheritance chain β€” she's the main character of this whole story fr fr. πŸ‘‘


πŸŽ€ USAGE πŸŽ€

The Class::MixinFactory package is just a facade β€” she's pretty on the outside, but her whole job is loading the necessary classes and providing a few import options for compile-time convenience. lowkey a socialite. πŸ’ƒ

πŸ’… Factory Interface

To generate an object with some combination of mixins, you first pass the names of the mixin classes to a class factory which will generate a mixed class. (Or return the name of the already generated class, if there has been a previous request with the same combination of mixins.) caching queen, we love her. ⭐

You can add a factory method to your base class, create a separate factory object, or inherit to produce a factory class. three flavors, pick your fighter. 🌈

πŸ“ Factory Method

To add a factory method to a base class, inherit from the Class::MixinFactory::HasAFactory class, or use the -hasafactory import option:

package MyClass;
use Class::MixinFactory -hasafactory;

package main;
my $class = MyClass->class( 'Logging', 'UpperCase' );
print $class->new()->foo();

🧚 Factory Class

To create a new class which will act as a factory for another base class, inherit from the Class::MixinFactory::Factory class, or use the -isafactory import option:

package MyClass::Factory;
use Class::MixinFactory -isafactory;
MyClass::Factory->base_class( "MyClass" );

package main;
my $class = MyClass::Factory->class( 'Logging', 'UpperCase' );
print $class->new()->foo();

πŸ‘‘ Factory Object

To create an object which will act as a factory, create a Class::MixinFactory::Factory instance by calling the new() method:

use Class::MixinFactory;
my $factory = Class::MixinFactory->new();
$factory->base_class( "MyClass" );

my $class = $factory->class( 'Logging', 'UpperCase' );
print $class->new()->foo();

🌈 Inheriting from a Mixed Class

πŸ’œ Inheriting with a Factory Method or Factory Object

A subclass can inherit from a mixed class:

package MyClass::CustomWidget;
@ISA = MyClass->class( 'Logging', 'UpperCase' );
sub foo { local $_ = (shift)->NEXT('foo', @_); tr[a-z][z-a]; $_ }

package main;
print MyClass::CustomWidget->new()->foo();

πŸ’– Inheriting with a Factory Class

A subclass can use a factory class to define its own inheritance:

package MyClass::CustomWidget;
use Class::MixinFactory -isasubclass,
      MyClass::Factory => 'Logging', 'UpperCase';
sub foo { local $_ = (shift)->NEXT('foo', @_); tr[a-z][z-a]; $_ }

package main;
print MyClass::CustomWidget->new()->foo();

πŸ”₯ Configuring a Factory

Factories support methods that control which classes they will use. it's giving settings app, but make it Perl. πŸŽ›οΈ

The base class will be inherited from by all mixed classes.

$factory->base_class( "HelloWorld" );

The mixin prefix is prepended to the mixin names passed to the class() method. Mixin names that contain a "::" are assumed to be fully qualified and are not changed. If empty, the base_class is used.

$factory->mixin_prefix( 'HelloFeature' );

The mixed prefix is at the start of all generated class names. If empty, the base_class is used, or the factory's class name.

$factory->mixed_prefix( 'HelloClass' );

🌸 Writing a Mixin Class

Writing a mixin class is almost the same as writing a subclass β€” except for where methods need to redispatch to the base-class implementation. and bestie, listen: the SUPER::method syntax will only search for classes that the mixin itself inherits from; to search back up the inheritance tree and explore other branches, another redispatch mechanism is needed. so SUPER:: is not the one here, sorry to her. πŸ™ˆ

A method named NEXT is provided to continue the search through to the next class which provides a given method. The order in which mixins are stacked is significant, so the caller should understand how their behaviors interact. (See Class::MixinFactory::NEXT.)

⚠️ fr fr, stack order is not a vibe check you can skip β€” know how your mixins interact before you ship it. πŸ’€

⭐ SEE ALSO ⭐

For distribution, installation, support, copyright and license information, see Class::MixinFactory::ReadMe. πŸ“š


and that's the whole page, bestie β€” perl v5.36.0, dated 2022-10-22, brought to you by the MixinFactory(3pm) girlies. slay, go build something cursed with multiple inheritance responsibly. πŸ’…βœ¨πŸŽ€

πŸ“¦ where this page comes from