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

In reply to Re: Attribute::Handlers usage at runtime by Anonymous Monk
in thread Attribute::Handlers usage at runtime by saintmike

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.