Dynamic class mixing
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::");
The \*(C`mix_class\*(C' function provided by this module dynamically generates `anonymous' classes with specified inheritance.
This function is used to dynamically generate `anonymous' classes by mixing pre-existing classes. This is useful where an incomplete class requires use of a mixin in order to become instantiable, several suitable mixins are available, and it is desired to make the choice between mixins at runtime. Each \s-1ITEM\s0 in the argument list is either the name of a class to inherit from (a parent class) or a reference to a hash of options. The @ISA list of the mixture class is set to the list of parent class names, in the order supplied. The options that may be supplied are:
Specifies the desired method resolution order (\s-1MRO\s0) of the mixture class. See mro for details of the valid values and the default determined by Perl. Typically, this should be set to c3 if mixing into an existing C3-based class hierarchy.
Specifies where the resulting package will go. May be \*(C`undef\*(C' to indicate that the caller doesn't care (which is the default state). Otherwise it must be either the empty string (to create a top-level package) or a bareword followed by \*(L"::\*(R" (to create a package under that name). For example, \*(L"Digest::\*(R" could be specified to ensure that the resulting package has a name starting with \*(L"Digest::\*(R", so that \*(C`Digest->new\*(C' 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. The same class will be returned by repeated invocations with the same parent class list and options. The returned name may be used to call a constructor or other class methods of the mixed class. A class name must be returned because there is no such thing as an anonymous class in Perl. Classes are referenced by name. The names that are generated by this function are unique and insignificant. See \*(C`genpkg\*(C' below for more information. If fewer than two classes to inherit from are specified, 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 \*(C`UNIVERSAL\*(C' may be returned. This provides the desired inheritance 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 want to modify your dynamically-generated `anonymous' classes, use \*(C`genpkg\*(C' (below).
This function selects and returns a package name that has not been previously used. The name returned is an ordinary bareword-form package name, and can be used as the second argument to \*(C`bless\*(C' and in all other ways that package names are used. The package is initially empty. The package names returned by this function are of a type that should not be used as ordinary fixed module names. However, it is not possible to entirely prevent a clash. This function checks that the package name it is about to return has not already been used, and will avoid returning such names, but it cannot guarantee that a later-loaded module will not create a clash. \s-1PREFIX\s0, 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 \*(L"::\*(R" (to create a package under that name). For example, \*(L"Digest::\*(R" could be specified to ensure that the resulting package has a name starting with \*(L"Digest::\*(R", so that \*(C`Digest->new\*(C' will accept it as the name of a message digest algorithm. If the \s-1PREFIX\s0 is not supplied, the caller is not expressing any preference.
Class::Generate, mro
Andrew Main (Zefram) <[email protected]>
Copyright (C) 2004, 2006, 2009, 2010, 2011 Andrew Main (Zefram) <[email protected]>
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.