Attributes: Introduction
Attributes appeared in Perl quite a while ago. Significant for old-timers, version Perl 5.005 contained a rather useless pragma
Now they would probably be called not attributes, but tags. Everything looks simple - several labels can be added to the declared variable or function. There are several standard Perl attributes, but you can enter your own. The built-in attributes are written in lowercase, and therefore it is not recommended to use such a register for custom labels - in order to avoid overlays in the future and present warings.
The attribute syntax is fairly clear: the Interpreter does not impose restrictions on the presence of a space between the colon and the attribute name, personally it seems to me more visual when they are written together. Attributes can be hung on functions and on variables - scalars, hashes, arrays.
For examples, go not far - on CPAN lives a group of modules, united under the name
The first of these
The syntax that it offers is much more clear: The
second class ,,
Third class,
Just put an attribute on the method
Interesting? Then do not forget to look into the source code of the modules.
UPD : continuation of the article: Attributes: a look inside
attrs
. This pragma made it possible to hang a couple of built-in attributes on a function, for which it was necessary to specify inside this function use attrs
with the corresponding parameters. However, already in March 2000 in Perl 5.6 this pragma was declared deprecated. It was replaced by a module attributes
, as well as an extended syntax for declaring variables / functions. It became possible to create custom labels for them and enter handlers for these labels. For unknown reasons, the attributes have not gained popularity. It's a pity.Closer to the point
Now they would probably be called not attributes, but tags. Everything looks simple - several labels can be added to the declared variable or function. There are several standard Perl attributes, but you can enter your own. The built-in attributes are written in lowercase, and therefore it is not recommended to use such a register for custom labels - in order to avoid overlays in the future and present warings.
The attribute syntax is fairly clear: the Interpreter does not impose restrictions on the presence of a space between the colon and the attribute name, personally it seems to me more visual when they are written together. Attributes can be hung on functions and on variables - scalars, hashes, arrays.
my $myVar : myAttribute = 10;
sub mySub : myAttribute1 : myAttribute2 {:}
What is it for
For examples, go not far - on CPAN lives a group of modules, united under the name
Attribute::Util
. I will not comment on the possibility of their application in real projects, but as a simple and clear illustration this is what you need. The first of these
Attribute::Abstract
,, offers a replacement for the classical description of abstract methods. Pay attention to it if you have similar declarations: sub myMethod { die "Abstract method called!"; }
The syntax that it offers is much more clear: The
sub myMethod: Abstract;
second class ,,
Attribute::Memoize
allows you to add caching to functions that return a constant result for the same sets of arguments. Of course, wrapping the function code in the if
-block is not difficult, but again - you can simply mark the function with an attribute :Memoize
and know that its result is cached. Third class,
Attribute::Method
, allows you to get rid of writing the first line of almost any method: my $self = shift;
Just put an attribute on the method
:Method
, and it $self
appears by itself. In addition, you can specify which arguments are passed to the method - then they will also be initialized: sub myMethod :Method($x) { print "self=$self, x=$x\n" }
Interesting? Then do not forget to look into the source code of the modules.
UPD : continuation of the article: Attributes: a look inside