I am interested specifically in the event captured by Sub::Mutate::when_sub_bodied(). I would like to be able to mess with a sub immediately after it has been compiled, i.e. its prototype has been set, its attributes have been executed, and its body has been defined, but before any code has had an opportunity to invoke it. I imagine the B::Hooks::EndOfScope::on_scope_end() approach would fail e.g. if there is a BEGIN block calling the sub immediately after its definition.
Cheers, -- Peter >________________________________ > From: Ben Tilly <bti...@gmail.com> >To: Peter Shangov <pshan...@yahoo.com> >Cc: Jesse Luehrs <d...@tozt.net>; moose <moose@perl.org> >Sent: Thursday, 12 January 2012, 17:12 >Subject: Re: "automatically" wrapping methods. > >This does not look like a sane thing to try to do. > >And what is your definition of "finished compiling"? Is every closure >instantiation a new compilation? > >On Thu, Jan 12, 2012 at 2:47 AM, Peter Shangov <pshan...@yahoo.com> wrote: >> Is there a sane way to specify a callback to be executed every time a >> subroutine has finished compiling? The `signatures` pragma currently does >> this but with XS, which is beyond my hacking abilities. If there is >> something like an API for hooking into subroutine creation, sugar method >> keywords could thrigger it too. >> >> -- >> Peter >> >> >> >> >>>________________________________ >>> From: Jesse Luehrs <d...@tozt.net> >>>To: Peter Shangov <pshan...@yahoo.com> >>>Cc: moose <moose@perl.org> >>>Sent: Wednesday, 11 January 2012, 22:05 >>>Subject: Re: "automatically" wrapping methods. >>> >>>On Wed, Jan 11, 2012 at 01:58:40PM -0800, Peter Shangov wrote: >>>> Or you can use B::Hooks::EndOfScope to do the wrapping at the end of the >>>> compilation stage: >>>> >>>> # untested code >>>> package MooseX::MethodValidation; >>>> use B::Hooks::EndOfScope; >>>> sub import { >>>> on_scope_end { >>>> my $caller = caller; >>>> my $meta = Moose::Meta::Class->initialize($caller); >>>> foreach my $method ( $meta->get_all_methods ) { >>>> ... # wrapping code goes here >>>> } >>>> } >>>> } >>>> >>>> package MyModel::User; >>>> use MooseX::MethodValidation; >>>> ... # your subs go here >>> >>>This actually isn't a great idea, because not all methods are defined at >>>compile time. If you declare any methods via method modifiers, or >>>attributes, or any other method declaration sugar, they won't exist when >>>that validation code runs. >>> >>>-doy >>> >>> >>> > > >