bennymack has asked for the wisdom of the Perl Monks concerning the following question:
Dear Perl Monks,
I have what appears to be a fairly difficult to solve problem with Attribute::Handlers. When a module that has some attributes defined is required at runtime, the attribute handling code does not run. I've looked through the Attribute::Handlers source and it seems like it only does its thing in a couple of specific compiler phases, namely CHECK and INIT (and END too.) So after those are complete it simply does not do anymore attribute handling.
I put together an example of what I am trying to get to work and posed the question via paste bin on the irc.freenode.net #perl channel. Here is the code for your viewing convenience:
############### SuperClass.pm package SuperClass; use strict; use warnings; use Attribute::Handlers(); # Keeping this simple for now. sub WrapSub : ATTR(CODE) { my( $class, $typeglob, $referent, ) = @_; my( $package_sub ) = sprintf '%s::%s', $class, *{ $typeglob }{NAME +}; my $wrap_sub = sub { warn 'before'; $referent->( @_ ); warn 'after'; }; warn 'WrapSub wrapping: ', $package_sub; no warnings 'redefine'; *{ $typeglob } = $wrap_sub; return; } 1; #################### SubClass.pm package SubClass; use strict; use warnings; use parent 'SuperClass'; sub foo : WrapSub { warn 'fooooo'; } 1; #!/usr/bin/env perl #################### test_attribute_handlers_compiletime.pl use strict; use warnings; use SubClass(); SubClass->foo; __END__ $ ./test_attribute_handlers_compiletime.pl WrapSub wrapping: SubClass::foo at SuperClass.pm line 17. before at SuperClass.pm line 12. fooooo at SubClass.pm line 7. after at SuperClass.pm line 14. #!/usr/bin/env perl #################### test_attribute_handlers_runtime.pl use strict; use warnings; use UNIVERSAL::require(); SubClass->require; SubClass->foo; __END__ $ ./test_attribute_handlers_runtime.pl fooooo at SubClass.pm line 7.
Is there a simple or not-so-simple solution to this? Such as modifying A::H, using a different handler module, or writing my own MODIFY_CODE_ATTRIBUTES method? Thanks in advance!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Attribute Handlers don't run when called at run time.
by tilly (Archbishop) on Sep 04, 2008 at 15:44 UTC | |
by bennymack (Pilgrim) on Sep 04, 2008 at 15:56 UTC | |
by tilly (Archbishop) on Sep 04, 2008 at 17:15 UTC |