diff options
author | Yves Orton <demerphq@gmail.com> | 2022-12-19 19:32:03 +0100 |
---|---|---|
committer | Yves Orton <demerphq@gmail.com> | 2023-03-18 20:57:59 +0800 |
commit | 93f6f9654a81b66c47c07ca982e4c00558bd4159 (patch) | |
tree | eb352f43ebad63b8813f6b3ef7b2084fd91df13f /mg_vtable.h | |
parent | 2f920c2f73ae58b754ccf1d897f1104e0cc3a4c6 (diff) | |
download | perl-93f6f9654a81b66c47c07ca982e4c00558bd4159.tar.gz |
pp_ctl.c - add support for hooking require.
This defines a new magic hash C<%{^HOOK}> which is intended to be used for
hooking keywords. It is similar to %SIG in that the values it contains
are validated on set, and it is not allowed to store something in
C<%{^HOOK}> that isn't supposed to be there. Hooks are expected to be
coderefs (people can use currying if they really want to put an object
in there, the API is deliberately simple.)
The C<%{^HOOK}> hash is documented to have keys of the form
"${keyword}__${phase}" where $phase is either "before" or "after"
and in this initial release two hooks are supported,
"require__before" and "require__after":
The C<require__before> hook is called before require is executed,
including any @INC hooks that might be fired. It is called with the path
of the file being required, just as would be stored in %INC. The hook
may alter the filename by writing to $_[0] and it may return a coderef
to be executed *after* the require has completed, otherwise the return
is ignored. This coderef is also called with the path of the file which
was required, and it will be called regardless as to whether the require
(or its dependencies) die during execution. This mechanism makes it
trivial and safe to share state between the initial hook and the coderef
it returns.
The C<require__after> hook is similar to the C<require__before> hook
however except that it is called after the require completes
(successfully or not), and its return is ignored always.
Diffstat (limited to 'mg_vtable.h')
-rw-r--r-- | mg_vtable.h | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/mg_vtable.h b/mg_vtable.h index bdf8187e55..a0273831bf 100644 --- a/mg_vtable.h +++ b/mg_vtable.h @@ -56,6 +56,8 @@ #define PERL_MAGIC_nonelem 'Y' /* Array element that does not exist */ #define PERL_MAGIC_defelem 'y' /* Shadow "foreach" iterator variable / smart parameter vivification */ +#define PERL_MAGIC_hook 'Z' /* %{^HOOK} hash */ +#define PERL_MAGIC_hookelem 'z' /* %{^HOOK} hash element */ #define PERL_MAGIC_lvref '\\' /* Lvalue reference constructor */ #define PERL_MAGIC_checkcall ']' /* Inlining/mutation of call to this CV */ #define PERL_MAGIC_extvalue '^' /* Value magic available for use by extensions */ @@ -75,6 +77,8 @@ enum { /* pass one of these to get_vtbl */ want_vtbl_envelem, want_vtbl_hints, want_vtbl_hintselem, + want_vtbl_hook, + want_vtbl_hookelem, want_vtbl_isa, want_vtbl_isaelem, want_vtbl_lvref, @@ -114,6 +118,8 @@ EXTCONST char * const PL_magic_vtable_names[magic_vtable_max] = { "envelem", "hints", "hintselem", + "hook", + "hookelem", "isa", "isaelem", "lvref", @@ -176,6 +182,8 @@ EXT_MGVTBL PL_magic_vtables[magic_vtable_max] = { { 0, Perl_magic_setenv, 0, Perl_magic_clearenv, 0, 0, 0, 0 }, { 0, 0, 0, Perl_magic_clearhints, 0, 0, 0, 0 }, { 0, Perl_magic_sethint, 0, Perl_magic_clearhint, 0, 0, 0, 0 }, + { 0, Perl_magic_sethookall, 0, Perl_magic_clearhookall, 0, 0, 0, 0 }, + { 0, Perl_magic_sethook, 0, Perl_magic_clearhook, 0, 0, 0, 0 }, { 0, Perl_magic_setisa, 0, Perl_magic_clearisa, 0, 0, 0, 0 }, { 0, Perl_magic_setisa, 0, 0, 0, 0, 0, 0 }, { 0, Perl_magic_setlvref, 0, 0, 0, 0, 0, 0 }, @@ -224,6 +232,8 @@ EXT_MGVTBL PL_magic_vtables[magic_vtable_max]; #define PL_vtbl_fm PL_magic_vtables[want_vtbl_fm] #define PL_vtbl_hints PL_magic_vtables[want_vtbl_hints] #define PL_vtbl_hintselem PL_magic_vtables[want_vtbl_hintselem] +#define PL_vtbl_hook PL_magic_vtables[want_vtbl_hook] +#define PL_vtbl_hookelem PL_magic_vtables[want_vtbl_hookelem] #define PL_vtbl_isa PL_magic_vtables[want_vtbl_isa] #define PL_vtbl_isaelem PL_magic_vtables[want_vtbl_isaelem] #define PL_vtbl_lvref PL_magic_vtables[want_vtbl_lvref] |