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

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

Thread Previous | Thread Next
From:
Ovid
Date:
August 31, 2022 07:37
Subject:
Re: Pre-RFC: A Metaprogramming (Reflection and Introspection) API
Message ID:
CA+M4CHu=4xvBsCpOs7R7dNigd_aO-RREx6qsUzcrpRqmUaBgqQ@mail.gmail.com
On Fri, Aug 26, 2022 at 7:30 PM Paul "LeoNerd" Evans <leonerd@leonerd.org.uk>
wrote:

>
>   * Create, manipulate, and inspect named symbols (variables and
>     functions) in named packages
>   * Create, manipulate, and inspect packages themselves
>   * Inspecting and manipulating lexicals captured by functions and
>     closures
>   * Create, manipulate, and inspect fields and methods of classes
>   * Create, manipulate, and inspect classes themselves
>   * Interacting with the compiler while it compiles code; for example
>     to inject callbacks at various occasions
>
> Plus I'm open to the idea I may still have missed some useful
> abilities.


In addition to this, I'd love to see native call stack from management and
filtering. Walking back up the stack can be expensive (particularly if
you're also trying to get the values of arguments passed in to a given sub)
and error-prone. Like many developers, I'm often using caller() to walk the
stack, but only in the contexts I need. For example, here's one I wrote
just yesterday:

    sub get_callpoint ($target_re) {
        my $frame          = 1;
        my $target_package = '<unknown>';
        my ( $package, $line );
        FRAME: while (1) {
            ( $package, undef, $line ) = caller($frame);
            $frame++;
            last FRAME unless $package;
            next FRAME if $package !~ /$target_re/;
            $target_package = $package;
            last FRAME;
        }
        return ($target_package, $line, $frame);
    };

I do similar things for building stack traces that only trace back to the
point of my tests and don't dump out huge amounts of lines from Catalyst,
Moose, etc: those lines are not relevant to me most of the time.

Instead, having a native Callstack class which handles all of this for us,
rather than again writing procedural code via caller() would be lovely.
Having it written in C rather than Perl might improve performance (though
when I need a callstack, performance probably isn't my first concern).

    my $stack = Callstack->new->until( package => qr/.../ )->reverse;

    # I like stacks from point of origin to target
    while ( my $frame = $stack->next ) {
        ...
    }

Best,
Curtis "Ovid" Poe
--
CTO, All Around the World
World-class software development and consulting
https://allaroundtheworld.fr/

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About