in reply to Trying to figure out subroutine attributes

That seems like a roundabout way of doing it. Instead, it would probably be easier to use your handler to maintain a table of available methods. The following is untested but should be close:

my %RPC_METHODS; sub xmlrpc_methods { my $package = shift; return keys %{ $RPC_METHODS{$package} }; } sub xmlrpc : ATTR(CODE) { my ( $package, $symbol ) = @_; my $name = *{$symbol}{NAME}; $RPC_METHODS{$package}{$name} = 1; }

Replies are listed 'Best First'.
Re^2: Trying to figure out subroutine attributes
by Anonymous Monk on Dec 08, 2006 at 22:12 UTC
    Thanks friedo. I perfer your solution. Do you know why my solution does not work though? Just so I understand what is going wrong and learn something in the process.
      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.]

        Ah. So the attributes themselves are no longer available after compile time. I didn't get that from the docs... :)