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

I want to use sub attributes to mark some of my subs as available over xmlrpc. The I would have thought the code below does what I want, but attributes::get always returns an empty list.
package Dispatcher; use strict; use warnings; use Attribute::Handlers; sub xmlrpc_methods { my @methods = (); my $package = shift; { no strict 'refs'; for my $key ( %{$package.'::'} ) { next unless my $code = *{$package.'::'.$key}{CODE}; push @methods, $key if grep { $_ eq 'xmlrpc' } attributes::get($code); } } return @methods; } 1; sub xmlrpc :ATTR(CODE) {} package MyClass; use base qw(Dispatcher); sub foo : xmlrpc { } 1;
I expect MyClass->xmlrpc_methods() to return foo, but it returns an empty list. Ideas? Thank you.

Replies are listed 'Best First'.
Re: Trying to figure out subroutine attributes
by friedo (Prior) on Dec 08, 2006 at 21:59 UTC

    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; }
      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.]