Front page | perl.perl5.porters |
Postings from August 2022
Pre-RFC: A Metaprogramming (Reflection and Introspection) API
Thread Next
From:
Paul "LeoNerd" Evans
Date:
August 26, 2022 17:30
Subject:
Pre-RFC: A Metaprogramming (Reflection and Introspection) API
Message ID:
20220826182954.2212000e@shy.leonerd.org.uk
There's a growing collection of code in various places which all has to
do with the subject of metaprogramming; that is, treating the code and
structure of a program itself like data that it operates on. As a
simple example, since basically forever core perl has permitted
manipulation of the symbol table in a `no strict 'refs'` scope, by
special syntax operating on globrefs. Often such trickery happens at
BEGIN time.
BEGIN {
no strict 'refs';
*name = sub { ... };
}
Many of the abilities that are relied on in real code are scattered
across a number of CPAN modules, even though the behaviours they
provide are fundamental language-level operations that should be
considered core behaviour. I think it would be nice to collect a lot of
these together and put them somewhere provided by core, so code can
rely on it, and its implementation can be closer tied to core.
In addition, the more we progress the feature-class branch, the more it
becomes apparent that a "MOP API" is required. This would give calling
code the ability to meta-program over class definitions and so on. It
feels like many of these new additions ought to go in the same place.
I'd therefore like to propose the following:
* We create a collection of core-provided functions for performing
metaprogramming activities, all together in one standard place.
* That place /could/ be the `builtin::` namespace we already
provided, but more likely I think it should live in yet another new
name, possibly as a regular kind of XS module - just one that
happens to ship with the core distribution. Name to be debated.
In addition, the list of abilities it needs to provide should probably
include:
* Create, manipulate, and inspect named symbols (variables and
functions) in named packages
-- as per using globrefs in core perl under no strict 'refs'
-- as per Package::Stash
* Create, manipulate, and inspect packages themselves
-- as per operating on %{"Package::Name::"} stashes under
no strict 'refs';
-- as per Package::Stash
* Inspecting and manipulating lexicals captured by functions and
closures
-- as per PadWalker
* Create, manipulate, and inspect fields and methods of classes
* Create, manipulate, and inspect classes themselves
-- as per Object::Pad::MOP::Class and friends
* Interacting with the compiler while it compiles code; for example
to inject callbacks at various occasions
-- as per B::Hooks::EndOfScope
-- as per B::CompilerPhase::Hook
Plus I'm open to the idea I may still have missed some useful
abilities. The point of the RFC is not to create a fully-exhaustive
list, but to create the module with enough useful pieces to begin with,
in the hope that more will be added over time as the need becomes
apparent.
In my pre-pre-RFC post I did mention B::Concise and B::Deparse, but they
concern themselves with the optrees of functions. Having a more robust
API for working on optrees from pureperl space would be useful, but it's
quite a significant undertaking in itself. I think for now I'd like to
consider that beyond the scope of this particular (pre)-RFC, and defer
it to a later one.
I will leave until the actual RFC discussions of the actual module name
for this collection, as well as the exact set of functions it provides,
their names, arguments, and specific behaviour. It is likely to take a
while to fine-tune it down to a workable idea.
Does this sound like a useful RFC to write? Shall I proceed?
--
Paul "LeoNerd" Evans
leonerd@leonerd.org.uk | https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/ | https://www.tindie.com/stores/leonerd/
Thread Next
-
Pre-RFC: A Metaprogramming (Reflection and Introspection) API
by Paul "LeoNerd" Evans