| 1 |
.\" -*- mode: troff; coding: utf-8 -*- |
| 2 |
.\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45) |
| 3 |
.\" |
| 4 |
.\" Standard preamble: |
| 5 |
.\" ======================================================================== |
| 6 |
.de Sp \" Vertical space (when we can't use .PP) |
| 7 |
.if t .sp .5v |
| 8 |
.if n .sp |
| 9 |
.. |
| 10 |
.de Vb \" Begin verbatim text |
| 11 |
.ft CW |
| 12 |
.nf |
| 13 |
.ne \\$1 |
| 14 |
.. |
| 15 |
.de Ve \" End verbatim text |
| 16 |
.ft R |
| 17 |
.fi |
| 18 |
.. |
| 19 |
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. |
| 20 |
.ie n \{\ |
| 21 |
. ds C` "" |
| 22 |
. ds C' "" |
| 23 |
'br\} |
| 24 |
.el\{\ |
| 25 |
. ds C` |
| 26 |
. ds C' |
| 27 |
'br\} |
| 28 |
.\" |
| 29 |
.\" Escape single quotes in literal strings from groff's Unicode transform. |
| 30 |
.ie \n(.g .ds Aq \(aq |
| 31 |
.el .ds Aq ' |
| 32 |
.\" |
| 33 |
.\" If the F register is >0, we'll generate index entries on stderr for |
| 34 |
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index |
| 35 |
.\" entries marked with X<> in POD. Of course, you'll have to process the |
| 36 |
.\" output yourself in some meaningful fashion. |
| 37 |
.\" |
| 38 |
.\" Avoid warning from groff about undefined register 'F'. |
| 39 |
.de IX |
| 40 |
.. |
| 41 |
.nr rF 0 |
| 42 |
.if \n(.g .if rF .nr rF 1 |
| 43 |
.if (\n(rF:(\n(.g==0)) \{\ |
| 44 |
. if \nF \{\ |
| 45 |
. de IX |
| 46 |
. tm Index:\\$1\t\\n%\t"\\$2" |
| 47 |
.. |
| 48 |
. if !\nF==2 \{\ |
| 49 |
. nr % 0 |
| 50 |
. nr F 2 |
| 51 |
. \} |
| 52 |
. \} |
| 53 |
.\} |
| 54 |
.rr rF |
| 55 |
.\" ======================================================================== |
| 56 |
.\" |
| 57 |
.IX Title "PERLMROAPI 1" |
| 58 |
.TH PERLMROAPI 1 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide" |
| 59 |
.\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 60 |
.\" way too many mistakes in technical documents. |
| 61 |
.if n .ad l |
| 62 |
.nh |
| 63 |
.SH NAME |
| 64 |
perlmroapi \- Perl method resolution plugin interface |
| 65 |
.SH DESCRIPTION |
| 66 |
.IX Header "DESCRIPTION" |
| 67 |
As of Perl 5.10.1 there is a new interface for plugging and using method |
| 68 |
resolution orders other than the default (linear depth first search). |
| 69 |
The C3 method resolution order added in 5.10.0 has been re-implemented as |
| 70 |
a plugin, without changing its Perl-space interface. |
| 71 |
.PP |
| 72 |
Each plugin should register itself by providing |
| 73 |
the following structure |
| 74 |
.PP |
| 75 |
.Vb 7 |
| 76 |
\& struct mro_alg { |
| 77 |
\& AV *(*resolve)(pTHX_ HV *stash, U32 level); |
| 78 |
\& const char *name; |
| 79 |
\& U16 length; |
| 80 |
\& U16 kflags; |
| 81 |
\& U32 hash; |
| 82 |
\& }; |
| 83 |
.Ve |
| 84 |
.PP |
| 85 |
and calling \f(CW\*(C`Perl_mro_register\*(C'\fR: |
| 86 |
.PP |
| 87 |
.Vb 1 |
| 88 |
\& Perl_mro_register(aTHX_ &my_mro_alg); |
| 89 |
.Ve |
| 90 |
.IP resolve 4 |
| 91 |
.IX Item "resolve" |
| 92 |
Pointer to the linearisation function, described below. |
| 93 |
.IP name 4 |
| 94 |
.IX Item "name" |
| 95 |
Name of the MRO, either in ISO\-8859\-1 or UTF\-8. |
| 96 |
.IP length 4 |
| 97 |
.IX Item "length" |
| 98 |
Length of the name. |
| 99 |
.IP kflags 4 |
| 100 |
.IX Item "kflags" |
| 101 |
If the name is given in UTF\-8, set this to \f(CW\*(C`HVhek_UTF8\*(C'\fR. The value is passed |
| 102 |
direct as the parameter \fIkflags\fR to \f(CWhv_common()\fR. |
| 103 |
.IP hash 4 |
| 104 |
.IX Item "hash" |
| 105 |
A precomputed hash value for the MRO's name, or 0. |
| 106 |
.SH Callbacks |
| 107 |
.IX Header "Callbacks" |
| 108 |
The \f(CW\*(C`resolve\*(C'\fR function is called to generate a linearised ISA for the |
| 109 |
given stash, using this MRO. It is called with a pointer to the stash, and |
| 110 |
a \fIlevel\fR of 0. The core always sets \fIlevel\fR to 0 when it calls your |
| 111 |
function \- the parameter is provided to allow your implementation to track |
| 112 |
depth if it needs to recurse. |
| 113 |
.PP |
| 114 |
The function should return a reference to an array containing string SVs |
| 115 |
giving the names of parent classes in order. The names of the classes should |
| 116 |
be the result of calling \f(CWHvENAME()\fR on the stash. In those cases where |
| 117 |
\&\f(CWHvENAME()\fR returns null, \f(CWHvNAME()\fR should be used instead. |
| 118 |
.PP |
| 119 |
The caller is responsible for incrementing the reference count of the array |
| 120 |
returned if it wants to keep the structure. Hence, if you have created a |
| 121 |
temporary value that you keep no pointer to, \f(CWsv_2mortal()\fR to ensure that |
| 122 |
it is disposed of correctly. If you have cached your return value, then |
| 123 |
return a pointer to it without changing the reference count. |
| 124 |
.SH Caching |
| 125 |
.IX Header "Caching" |
| 126 |
Computing MROs can be expensive. The implementation provides a cache, in |
| 127 |
which you can store a single \f(CW\*(C`SV *\*(C'\fR, or anything that can be cast to |
| 128 |
\&\f(CW\*(C`SV *\*(C'\fR, such as \f(CW\*(C`AV *\*(C'\fR. To read your private value, use the macro |
| 129 |
\&\f(CWMRO_GET_PRIVATE_DATA()\fR, passing it the \f(CW\*(C`mro_meta\*(C'\fR structure from the |
| 130 |
stash, and a pointer to your \f(CW\*(C`mro_alg\*(C'\fR structure: |
| 131 |
.PP |
| 132 |
.Vb 2 |
| 133 |
\& meta = HvMROMETA(stash); |
| 134 |
\& private_sv = MRO_GET_PRIVATE_DATA(meta, &my_mro_alg); |
| 135 |
.Ve |
| 136 |
.PP |
| 137 |
To set your private value, call \f(CWPerl_mro_set_private_data()\fR: |
| 138 |
.PP |
| 139 |
.Vb 1 |
| 140 |
\& Perl_mro_set_private_data(aTHX_ meta, &c3_alg, private_sv); |
| 141 |
.Ve |
| 142 |
.PP |
| 143 |
The private data cache will take ownership of a reference to private_sv, |
| 144 |
much the same way that \f(CWhv_store()\fR takes ownership of a reference to the |
| 145 |
value that you pass it. |
| 146 |
.SH Examples |
| 147 |
.IX Header "Examples" |
| 148 |
For examples of MRO implementations, see \f(CWS_mro_get_linear_isa_c3()\fR |
| 149 |
and the \f(CW\*(C`BOOT:\*(C'\fR section of \fIext/mro/mro.xs\fR, and |
| 150 |
\&\f(CWS_mro_get_linear_isa_dfs()\fR in \fImro_core.c\fR |
| 151 |
.SH AUTHORS |
| 152 |
.IX Header "AUTHORS" |
| 153 |
The implementation of the C3 MRO and switchable MROs within the perl core was |
| 154 |
written by Brandon L Black. Nicholas Clark created the pluggable interface, |
| 155 |
refactored Brandon's implementation to work with it, and wrote this document. |