Attribute Lists - Page 25
July 27, 2001
Attributes are a largely experimental feature, still under
development, and only present from Perl version 5.6 onwards, so
accordingly we have left them to the end of the chapter. The use
of attributes in production code is not recommended, but being
aware of them is not a bad idea, since they will ultimately
mature into an official part of the language.
In brief, attributes are pieces of information associated with
either variables or subroutines that can be set to modify their
behavior in specific ways. The primary users of attributes are
subroutines. Perl recognizes and understands three special
attributes, lvalue, locked, and
method, which alter the way in which the Perl
interpreter executes subroutines. It is more than likely other
special attributes will appear as Perl evolves. We have already
seen lvalue in this chapter, and cover
locked and method in brief below.
Currently, we cannot define our own attributes on either
variables or subroutines, only those defined and understood by
Perl. However, an experimental package attribute mechanism, which
associates user-defined attributes with packages, is under
development. All variables and subroutines that reside in the
package automatically have the package attributes associated with
them.
Attributes can be placed on both subroutines and lexical
variables. The basic form of a variable attribute list is one or
more variables declared with my followed by a
semicolon and the attribute list. However, there are no variable
attributes currently understood by Perl.
Defining Attributes on Subroutines
The basic form of a subroutine attribute list is a standard
subroutine definition (or declaration), followed by a colon and
the attributes to be defined. Attributes are separated by
whitespace, and optionally a colon, these are then followed by
the body of the subroutine or (in the case of a declaration) a
semicolon:
sub mysubroutine : attr1 : attr2 { # standard subroutine
... body of subroutine ...
}
sub mysubroutine : attr1 attr2; # subroutine declaration
my $subref = sub : attr1 : attr2 { # anonymous subroutine
... body of subroutine ...
}
sub mysubroutine (\@$$;$) : attr;
# declaration with prototype
sub mysubroutine attr (parameters);
# attribute with parameters
At the time of writing, the attributes lvalue,
locked, and method are the only
attributes that can be set. None of these use a parameter list as
shown in the last example, but the syntax accepts the possibility
in anticipation of future applications.
Accessing Attributes
Attribute definitions are actually handled by the
attributes pragmatic module, which implements the
modified syntax for variables and subroutines that allows them to
be defined. The attributes module also supplies
subroutines to access these attributes, which we can use by
importing them from the attributes module:
use attributes qw(get reftype);
# import 'get' and 'reftype' subroutines
The get subroutine takes a variable, or subroutine
reference, and returns a list of attributes defined on that
reference. If there are none, then the list is empty. For
example:
sub mysubroutine : locked method {
...
}
my @attrlist = get \&mysubroutine;
# contains ('locked', 'method')
The reftype subroutine also takes a reference to a
variable or subroutine. It returns the underlying reference type;
HASH for a hash variable, CODE for a
subroutine reference, and so on. Blessed references return the
underlying data type, which makes reftype a
potentially useful subroutine as a replacement for
ref, even if we are not using attributes.
Assignable Subroutines - Page 24
Professional Perl Programming
Special Attributes - Page 26
|