saintmike has asked for the wisdom of the Perl Monks concerning the following question:

Esteemed monks, with a trick available through Attribute::Handlers it's possible to have a subroutine print out some tracing info before it gets called (derived from this article):
use Attribute::Handlers; sub myattr :ATTR { my($func, $symbol, $code) = @_; no warnings 'redefine'; *{$symbol} = sub { print "Before\n"; $code->(); } } sub somefunc :myattr { print "somefunc!\n"; } somefunc();
This will print "Before" every time somefunc will be called. Now, what I'd like to do is create a Module, say MyAttrMaker that would accomplish the above like this:
use MyAttrMaker qw(myattr); sub somefunc :myattr { print "somefunc!\n"; } somefunc();
For this to work, MyAttrMaker would have an import() function, which would use Attribute::Handlers and define sub myattr :ATTR {.

However, due to Attribute::Handler creating these hooks in the CHECK phase (other phases can be selected, too), this doesn't seem to be possible. Or is it?

Replies are listed 'Best First'.
Re: Attribute::Handlers usage at runtime
by liz (Monsignor) on Jun 13, 2004 at 09:22 UTC
Re: Attribute::Handlers usage at runtime
by Anonymous Monk on Jun 13, 2004 at 14:33 UTC

    If I understand your question correctly, then the following should make sense, otherwise... :-)

    @ISA is used to locate attribute handlers, so there are a few ways to do this. The way the perldocs suggest (and the way I have always seen it) would be:

    package MyAttrMaker;
    
    use Attribute::Handlers;
    
    sub UNIVERSAL::myattr :ATTR {
         ...
    }
    

    By putting the attribute into UNIVERSAL, the calling package will have it available in its @ISA tree. The downside to this is it is now available to all packages, regardless if they used MyAttrMaker.

    To overcome this you could write:

    package MyAttrMaker;
    
    use Attribute::Handlers;
    
    sub myattr :ATTR {
         ...
    }
    

    And then in your calling package use it like:

    use MyAttrMaker;
    use base MyAttrMaker;
    

    Now it will only be available to packages that use is explicitly (or their sub-packages/sub-classes).

    If you do not want to use base everytime you use the module, you could put the following import sub into your MyAttrMaker package:

    sub import {
         my $caller = caller;
         push @{$caller  . '::ISA'}, MyAttrMaker;
    }
    

    Now, all you have to say is use MyAttrMaker;.

    None of these solutions are perfect, but they are the way to do it, and they don't cause too many problems. None of them solve your other requirement, that you can choose which attributes to import when using the module. I cannot think of an easy way to pull this off right now.

    Ted