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

Dear monks,

once in a while I come across constructs like this:
sub frobnicate :Ultracool { ... }
I understand that :Ultracool is an 'Attribute', and I've read perldoc perlsub and perldoc attributes - but I don't quite understand why and how you would use them. What is gained? What are they for?

Replies are listed 'Best First'.
Re: Subs with 'extra stuff'?
by Corion (Patriarch) on Feb 22, 2006 at 09:17 UTC

    Attributes are like post-it notes you can tack onto anything in Perl. The technical term is "out of band data" I believe.

    Attributes are used extensively in Catalyst to declare the URLs to which a subroutine is bound, and I've used them to declare access control lists together with the subroutine. They work by giving you a callback when declaring the subroutine, so they are more or less syntactic sugar for:

    Ultracool->declare_subroutine('frobnicate',sub { ... }, qw(some more arguments));
Re: Subs with 'extra stuff'?
by friedo (Prior) on Feb 22, 2006 at 16:44 UTC
    Another example is Test::Class, which uses attribute handlers to register a list of methods in each class that need to be run.
Re: Subs with 'extra stuff'?
by Limbic~Region (Chancellor) on Feb 23, 2006 at 13:47 UTC
      Wow - that's really the exact same question, only better phrased :-)

      Thanks! Interesting discussion. I think you might use them for example to distinguish between types of methods, e.g.:
      sub set_name : Interface { croak "Not implemented!"; } sub get_name : Interface { croak "Not implemented!"; }
      So that you can query whether a method call you are about to make is implemented by the right class - and not by SUPER:: in an interface - without using eval { ... } blocks.
Re: Subs with 'extra stuff'?
by revdiablo (Prior) on Feb 23, 2006 at 03:30 UTC

    I haven't seen it mentioned yet, but this is documented in perlsub. See the "Subroutine Attributes" section for all the gory details.

    Update: oops, I just noticed from the OP:

    I've read perldoc perlsub and perldoc attributes

    Apologies for not reading the entire post.