develooper Front page | perl.perl5.porters | Postings from August 2022

Pre-RFC: A Metaprogramming (Reflection and Introspection) API

Thread Next
Paul "LeoNerd" Evans
August 26, 2022 17:30
Pre-RFC: A Metaprogramming (Reflection and Introspection) API
Message ID:
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.

    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

  * 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
      -- 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

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      |  |

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About