Class::Mix(3pm)

πŸ’– Class::Mix πŸ’–

okay so bestie, this is the Class::Mix page from the User Contributed Perl Documentation, and it's literally about dynamic class mixing ✨ buckle up πŸŽ€


✨ NAME ✨

Class::Mix β€” dynamic class mixing πŸ¦‹


🌸 SYNOPSIS 🌸

use Class::Mix qw(mix_class);

$foobar_object = mix_class("Foo", "Bar")->new;
$digest_class = mix_class("Foo", "Bar", {prefix=>"Digest::"});

use Class::Mix qw(genpkg);

$package = genpkg;
$package = genpkg("Digest::Foo::");

πŸ’… DESCRIPTION πŸ’…

The mix_class function that this module hands you dynamically generates anonymous classes with whatever inheritance you ask for. no cap 🫢


πŸ‘‘ FUNCTIONS πŸ‘‘

🌈 mix_class(ITEMS ...)

This one is the main character fr β€” it dynamically generates anonymous classes by mixing pre-existing classes. When is this giving?? When you've got an incomplete class that needs a mixin to even become instantiable, you've got several suitable mixins lying around, and you wanna pick between them at runtime. that's the moment πŸ’«

Every single ITEM in the argument list is either:

  • the name of a class to inherit from (aka a parent class), or
  • a reference to a hash of options.

The @ISA list of the mixture class gets set to the list of parent class names, in the order supplied. The options you may supply are:

  • mro β€” specifies the desired method resolution order (MRO) of the mixture class. Go read mro for the details of the valid values and the default that Perl determines. Typically you should set this to c3 if you're mixing into an existing C3-based class hierarchy.
  • prefix β€” specifies where the resulting package will go. May be undef to say the caller doesn't care (and that's the default state, bestie). Otherwise it must be either the empty string (to create a top-level package) or a bareword followed by :: (to create a package under that name). For example, you could specify Digest:: to make sure the resulting package has a name starting with Digest::, so that Digest->new will accept it as the name of a message digest algorithm. πŸ“

The function generates a class of the form described by the arguments, and returns its name. Repeated invocations with the same parent class list and options will return the same class. And yes, you can use that returned name to call a constructor or other class methods of the mixed class. slay πŸ’…

A class name must be returned because β€” and this is the tea β€” there is no such thing as an anonymous class in Perl. Classes are referenced by name, periodt. The names generated by this function are unique and insignificant. See genpkg below for more info.

If you specify fewer than two classes to inherit from, the function tries to avoid generating a separate class for the mixture. If only one parent class is specified, then that class may be returned, and if no parent classes are specified, then UNIVERSAL may be returned. This gives you the inheritance you want without creating superfluous classes πŸ‘‘ These special cases only apply if the options are compatible with the pre-existing class.

⚠️ This function relies on the classes it returns remaining unmodified in order to be returned by future invocations. If you wanna modify your dynamically-generated anonymous classes, use genpkg (below). don't do it, i'm serious 😭

🧚 genpkg([PREFIX])

This function selects and returns a package name that has not been previously used. The name it returns is an ordinary bareword-form package name, and you can use it as the second argument to bless and in all other ways that package names are used. The package starts out initially empty. ✨

The package names returned by this function are of a type that should not be used as ordinary fixed module names. But β€” and this is the honest part β€” it is not possible to entirely prevent a clash. This function checks that the package name it's about to return hasn't already been used, and it will avoid returning such names, but it cannot guarantee that a later-loaded module won't create a clash. πŸ™ˆ

PREFIX, if present, specifies where the resulting package will go. It must be either the empty string (to create a top-level package) or a bareword followed by :: (to create a package under that name). For example, Digest:: could be specified to ensure that the resulting package has a name starting with Digest::, so that Digest->new will accept it as the name of a message digest algorithm. If you don't supply PREFIX, then the caller isn't expressing any preference. πŸ¦‹


πŸ”₯ SEE ALSO πŸ”₯

  • Class::Generate
  • mro

🫢 AUTHOR 🫢

Andrew Main (Zefram) <zefram@fysh.org>


Copyright (C) 2004, 2006, 2009, 2010, 2011, 2017 Andrew Main (Zefram) <zefram@fysh.org>


🌈 LICENSE 🌈

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


and that's the whole page bestie, no notes πŸ’– this one shipped with perl v5.34.0 and got dated 2022-06-11 β€” go forth and mix those classes, you're literally iconic for reading the docs πŸ“šβœ¨πŸ‘‘

πŸ“¦ where this page comes from