in reply to Re^2: Trying to figure out subroutine attributes
in thread Trying to figure out subroutine attributes

Here's the only real problem:
sub xmlrpc :ATTR(CODE) {}
This says "for the attribute xmlrpc, do nothing". The code attached to the ATTR(CODE) is to be run at the time the code with that attribute is compiled. You have an empty block, so Perl does that: nothing at all.

The toughest part about using Attribute::Handlers is remembering that the attribute code gets control at compile time, and that the only way to influence execution later is to record information at that point.

Note that friedo's code is recording information in a hash in the sub xmlrpc: ATTR(CODE) subroutine, and the xmlrpc_methods sub then just reads it off. If you look at Class::AutoPlug::Plugin, you can see an example of using a similar mechanism to "advertise" methods and method hooks.

[I just noticed a documentation bug I have to fix there - the hooks actually get and send back a Class::AutoPlug::ResultState, instead of magical numeric values, wich allows the hooks to manipulate @_ and to replace the real return value for a method. However, this is unrelated to the attribute-based "advertising"; check out the source for the details on how that works. I'll be pushing another version with the doc corrections this evening.]

Replies are listed 'Best First'.
Re^4: Trying to figure out subroutine attributes
by Anonymous Monk on Dec 11, 2006 at 15:37 UTC
    Ah. So the attributes themselves are no longer available after compile time. I didn't get that from the docs... :)
      Yeah, it's like assembler (or LISP) macros. It lets you get into the compile phase and do stuff.

      I should clarify: You don't have to just store data; you can do anything that Perl lets you do. Create subroutines (or closures). Fire up WWW::Mechanize and load data from a Web server. Build a connection to your database automagically by having a scalar with the connection info in it and an attribute that takes the data, builds the connection, and drops it back into the scalar.