1PERLMROAPI(1) Perl Programmers Reference Guide PERLMROAPI(1)
2
3
4
6 perlmroapi - Perl method resolution plugin interface
7
9 As of Perl 5.10.1 there is a new interface for plugging and using
10 method resolution orders other than the default (linear depth first
11 search). The C3 method resolution order added in 5.10.0 has been re-
12 implemented as a plugin, without changing its Perl-space interface.
13
14 Each plugin should register itself by providing the following structure
15
16 struct mro_alg {
17 AV *(*resolve)(pTHX_ HV *stash, U32 level);
18 const char *name;
19 U16 length;
20 U16 kflags;
21 U32 hash;
22 };
23
24 and calling "Perl_mro_register":
25
26 Perl_mro_register(aTHX_ &my_mro_alg);
27
28 resolve
29 Pointer to the linearisation function, described below.
30
31 name
32 Name of the MRO, either in ISO-8859-1 or UTF-8.
33
34 length
35 Length of the name.
36
37 kflags
38 If the name is given in UTF-8, set this to "HVhek_UTF8". The value
39 is passed direct as the parameter kflags to hv_common().
40
41 hash
42 A precomputed hash value for the MRO's name, or 0.
43
45 The "resolve" function is called to generate a linearised ISA for the
46 given stash, using this MRO. It is called with a pointer to the stash,
47 and a level of 0. The core always sets level to 0 when it calls your
48 function - the parameter is provided to allow your implementation to
49 track depth if it needs to recurse.
50
51 The function should return a reference to an array containing string
52 SVs giving the names of parent classes in order. The names of the
53 classes should be the result of calling HvENAME() on the stash. In
54 those cases where HvENAME() returns null, HvNAME() should be used
55 instead.
56
57 The caller is responsible for incrementing the reference count of the
58 array returned if it wants to keep the structure. Hence, if you have
59 created a temporary value that you keep no pointer to, sv_2mortal() to
60 ensure that it is disposed of correctly. If you have cached your return
61 value, then return a pointer to it without changing the reference
62 count.
63
65 Computing MROs can be expensive. The implementation provides a cache,
66 in which you can store a single "SV *", or anything that can be cast to
67 "SV *", such as "AV *". To read your private value, use the macro
68 MRO_GET_PRIVATE_DATA(), passing it the "mro_meta" structure from the
69 stash, and a pointer to your "mro_alg" structure:
70
71 meta = HvMROMETA(stash);
72 private_sv = MRO_GET_PRIVATE_DATA(meta, &my_mro_alg);
73
74 To set your private value, call Perl_mro_set_private_data():
75
76 Perl_mro_set_private_data(aTHX_ meta, &c3_alg, private_sv);
77
78 The private data cache will take ownership of a reference to
79 private_sv, much the same way that hv_store() takes ownership of a
80 reference to the value that you pass it.
81
83 For examples of MRO implementations, see S_mro_get_linear_isa_c3() and
84 the "BOOT:" section of ext/mro/mro.xs, and S_mro_get_linear_isa_dfs()
85 in mro_core.c
86
88 The implementation of the C3 MRO and switchable MROs within the perl
89 core was written by Brandon L Black. Nicholas Clark created the
90 pluggable interface, refactored Brandon's implementation to work with
91 it, and wrote this document.
92
93
94
95perl v5.38.2 2023-11-30 PERLMROAPI(1)