Attributes: powerful Perl syntax you might not know

| No Comments

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:

Leave a comment

Job hunting for programmers


Land the Tech Job You Love, Andy Lester's guide to job hunting for programmers and other technical professionals, is available in PDF, ePub and .mobi formats, all DRM-free, as well as good old-fashioned paper.