Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Support for attributes:get in Attribute::Handlers?

by shmem (Chancellor)
on Feb 15, 2009 at 18:28 UTC ( [id://743949]=note: print w/replies, xml ) Need Help??


in reply to Support for attributes:get in Attribute::Handlers?

Yes.

use strict; package LoudDecl; use Attribute::Handlers; use attributes; sub FETCH_CODE_ATTRIBUTES { qw(Loud) } sub Loud :ATTR { print "NOISE!\n"; } sub foo: Loud method { print 1; } print "Attributes: "; print join ', ', attributes::get(\&foo); print "\n"; __END__ NOISE! Attributes: method, Loud

See attributes, section "Package-specific Attribute Handling".

update: if you define multiple attributes, you might want to return the right attributes associated to the sub questioned:

use strict; package LoudDecl; use Attribute::Handlers; use attributes; use Scalar::Util; my %attrs; sub FETCH_CODE_ATTRIBUTES { @{$attrs{Scalar::Util::refaddr($_[1])}}; } sub Loud :ATTR { print "NOISE!\n"; no strict 'refs'; push @{$attrs{Scalar::Util::refaddr($_[2])}}, 'Loud'; } sub Boom :ATTR { print "BOOM!\n"; no strict 'refs'; push @{$attrs{Scalar::Util::refaddr($_[2])}}, 'Boom'; } sub foo: Loud method { print 1; } sub bar : Loud Boom method { print "pling.\n"; } print "Attributes: "; print join ', ', attributes::get(\&bar); print "\n"; __END__ NOISE! NOISE! BOOM! Attributes: method, Loud, Boom

Replies are listed 'Best First'.
Re^2: Support for attributes:get in Attribute::Handlers?
by Thilosophy (Curate) on Feb 15, 2009 at 22:22 UTC
    shmem++ for this, although this is basically hand-rolling the support, without any help from Attribute::Handlers. It is also rather intrusive, in that you have to amend the original attribute handlers, which could be a problem if those are located in someone else's modules.

    I suppose this is how it is with attributes...

      Thank you. - yes, this is hand-rolling, a workaround, and IMHO Attribute::Handlers should shoehorn FETCH_<type>_ATTRIBUTES methods into the caller's namespace, if not already there yet. Best thing to do would be patching Attribute::Handlers, but since I haven't done much with attributes yet, and haven' got (yet) a full understanding neither of what's going on inside Attribute::Handlers nor why it is written as it is; and since the POD states

      BUGS
      There are undoubtedly serious bugs lurking somewhere in code this funky :-) Bug reports and other feedback are most welcome.

      it is quite easy for me to abstain from adding bugs - and patching code of TheDamian's make is a terrible thing to do... ;-)

      You could send TheDamian "other feedback", though.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://743949]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-03-29 04:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found