Attributes: powerful Perl syntax you might not know
An anonymous monk at PerlMonks asks How is this Perl?.
sub end : ActionClass('RenderView') {}
As it turns out, they're attributes, one of the newer and less well understood additions to Perl. You won't see them everywhere, but they're starting to appear in a number of places; many people -- including the anonymous perlmonk -- first encounter them in the Catalyst MVC framework.
Attributes allow you to add extra semantics to any Perl subroutine or variable. The typical example -- given in both the perlmonks thread linked above, and in the Attribute::Handlers docs linked below, is an attribute :Loud
which makes any subroutine called with it behave as if it only knew how to shout:
sub greet : Loud {
print "Good morning.n";
}
greet();
... prints "GOOD MORNING";
You can also declare variables using attributes:
my $greeting : Loud;
To learn more about attributes and how to write them:
perldoc attributes
- Attribute::Handlers -- a nicer way to handle attributes
- Lightning Strikes Four Times, where Mike Friedman discusses using attributes for logging.