1Class::Mix(3) User Contributed Perl Documentation Class::Mix(3)
2
3
4
6 Class::Mix - dynamic class mixing
7
9 use Class::Mix qw(mix_class);
10
11 $foobar_object = mix_class("Foo", "Bar")->new;
12 $digest_class = mix_class("Foo", "Bar", {prefix=>"Digest::"});
13
14 use Class::Mix qw(genpkg);
15
16 $package = genpkg;
17 $package = genpkg("Digest::Foo::");
18
20 The "mix_class" function provided by this module dynamically generates
21 `anonymous' classes with specified inheritance.
22
24 mix_class(ITEMS ...)
25 This function is used to dynamically generate `anonymous' classes
26 by mixing pre-existing classes. This is useful where an incomplete
27 class requires use of a mixin in order to become instantiable,
28 several suitable mixins are available, and it is desired to make
29 the choice between mixins at runtime.
30
31 Each ITEM in the argument list is either the name of a class to
32 inherit from (a parent class) or a reference to a hash of options.
33 The @ISA list of the mixture class is set to the list of parent
34 class names, in the order supplied. The options that may be
35 supplied are:
36
37 mro Specifies the desired method resolution order (MRO) of the
38 mixture class. See mro for details of the valid values and the
39 default determined by Perl. Typically, this should be set to
40 c3 if mixing into an existing C3-based class hierarchy.
41
42 prefix
43 Specifies where the resulting package will go. May be "undef"
44 to indicate that the caller doesn't care (which is the default
45 state). Otherwise it must be either the empty string (to
46 create a top-level package) or a bareword followed by "::" (to
47 create a package under that name). For example, "Digest::"
48 could be specified to ensure that the resulting package has a
49 name starting with "Digest::", so that "Digest->new" will
50 accept it as the name of a message digest algorithm.
51
52 The function generates a class of the form described by the
53 arguments, and returns its name. The same class will be returned
54 by repeated invocations with the same parent class list and
55 options. The returned name may be used to call a constructor or
56 other class methods of the mixed class.
57
58 A class name must be returned because there is no such thing as an
59 anonymous class in Perl. Classes are referenced by name. The
60 names that are generated by this function are unique and
61 insignificant. See "genpkg" below for more information.
62
63 If fewer than two classes to inherit from are specified, the
64 function tries to avoid generating a separate class for the
65 mixture. If only one parent class is specified then that class may
66 be returned, and if no parent classes are specified then
67 "UNIVERSAL" may be returned. This provides the desired inheritance
68 without creating superfluous classes. These special cases only
69 apply if the options are compatible with the pre-existing class.
70
71 This function relies on the classes it returns remaining unmodified
72 in order to be returned by future invocations. If you want to
73 modify your dynamically-generated `anonymous' classes, use "genpkg"
74 (below).
75
76 genpkg([PREFIX])
77 This function selects and returns a package name that has not been
78 previously used. The name returned is an ordinary bareword-form
79 package name, and can be used as the second argument to "bless" and
80 in all other ways that package names are used. The package is
81 initially empty.
82
83 The package names returned by this function are of a type that
84 should not be used as ordinary fixed module names. However, it is
85 not possible to entirely prevent a clash. This function checks
86 that the package name it is about to return has not already been
87 used, and will avoid returning such names, but it cannot guarantee
88 that a later-loaded module will not create a clash.
89
90 PREFIX, if present, specifies where the resulting package will go.
91 It must be either the empty string (to create a top-level package)
92 or a bareword followed by "::" (to create a package under that
93 name). For example, "Digest::" could be specified to ensure that
94 the resulting package has a name starting with "Digest::", so that
95 "Digest->new" will accept it as the name of a message digest
96 algorithm. If the PREFIX is not supplied, the caller is not
97 expressing any preference.
98
100 Class::Generate, mro
101
103 Andrew Main (Zefram) <zefram@fysh.org>
104
106 Copyright (C) 2004, 2006, 2009, 2010, 2011, 2017 Andrew Main (Zefram)
107 <zefram@fysh.org>
108
110 This module is free software; you can redistribute it and/or modify it
111 under the same terms as Perl itself.
112
113
114
115perl v5.36.0 2023-01-20 Class::Mix(3)