perl - Method modifiers and glob assignments -


sometimes moose's method modifiers not play symbol table entries created other packages try moose-like things own way.

i working older code follows pattern:

package methodcreator;  sub make_some_method {     $caller = caller();     *{$caller . '::generated_method'} = sub { print 'i generated method' } }  1; 

the intent of methodcreator package add standard definitions multiple consumer packages, , implements via direct glob assignment. problem is, these created methods not play moose's method modifiers:

package consumer;  use moose; use methodcreator;  methodcreator::make_some_method();  # following line causes compilation fail                                                                                                                                                               # before generated_method => sub { print 'about call generated method: ' };                                                                                                                               generated_method();  1; 

as comment indicates, attempt use method modifier on 1 of these dynamically-added subroutines results in compile time error ("generated_method not in inheritance hierarchy").

it not practical change or replace methodcreator (as may "right solution"). question is: how can package consumer changed make 'before' modifier play such subroutines, i.e. behave expect if 'generated_method' defined directly within consumer?

you can use class::method::modifiers instead of using built-in moose modifiers. make sure not import them, though, or redefined warnings. instead, call before explicitly class::method::modifiers package.

package methodcreator; use strict; use warnings; no strict 'refs';  sub make_some_method {     $caller = caller();     *{$caller . '::generated_method'} = sub { print 'i generated method' } }  package consumer;  use moose; use class::method::modifiers ();  methodcreator::make_some_method();  # 1 works class::method::modifiers::before( generated_method => sub { print 'about call generated method: ' });  generated_method();  1; 

output:

about call generated method: generated method 

now why work?

the c::m::m docs this:

note syntax , semantics these modifiers directly borrowed moose (the implementations, however, not).

simplified, overwrites sub in package own sub, stuff , calls original 1 afterwards.

in moose on other hand implemented in class::mop::method::wrapped, , use mop handle fancy inheritance stuff multiple modifiers. because don't have 'manually generated' subroutine/method, not work.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -