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!


In reply to Attribute Handlers don't run when called at run time. by bennymack

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.