1attributes(3pm)        Perl Programmers Reference Guide        attributes(3pm)
2
3
4

NAME

6       attributes - get/set subroutine or variable attributes
7

SYNOPSIS

9         sub foo : method ;
10         my ($x,@y,%z) : Bent = 1;
11         my $s = sub : method { ... };
12
13         use attributes ();    # optional, to get subroutine declarations
14         my @attrlist = attributes::get(\&foo);
15
16         use attributes 'get'; # import the attributes::get subroutine
17         my @attrlist = get \&foo;
18

DESCRIPTION

20       Subroutine declarations and definitions may optionally have attribute
21       lists associated with them.  (Variable "my" declarations also may, but
22       see the warning below.)  Perl handles these declarations by passing
23       some information about the call site and the thing being declared along
24       with the attribute list to this module.  In particular, the first
25       example above is equivalent to the following:
26
27           use attributes __PACKAGE__, \&foo, 'method';
28
29       The second example in the synopsis does something equivalent to this:
30
31           use attributes ();
32           my ($x,@y,%z);
33           attributes::->import(__PACKAGE__, \$x, 'Bent');
34           attributes::->import(__PACKAGE__, \@y, 'Bent');
35           attributes::->import(__PACKAGE__, \%z, 'Bent');
36           ($x,@y,%z) = 1;
37
38       Yes, that's a lot of expansion.
39
40       WARNING: attribute declarations for variables are still evolving.  The
41       semantics and interfaces of such declarations could change in future
42       versions.  They are present for purposes of experimentation with what
43       the semantics ought to be.  Do not rely on the current implementation
44       of this feature.
45
46       There are only a few attributes currently handled by Perl itself (or
47       directly by this module, depending on how you look at it.)  However,
48       package-specific attributes are allowed by an extension mechanism.
49       (See "Package-specific Attribute Handling" below.)
50
51       The setting of subroutine attributes happens at compile time.  Variable
52       attributes in "our" declarations are also applied at compile time.
53       However, "my" variables get their attributes applied at run-time.  This
54       means that you have to reach the run-time component of the "my" before
55       those attributes will get applied.  For example:
56
57           my $x : Bent = 42 if 0;
58
59       will neither assign 42 to $x nor will it apply the "Bent" attribute to
60       the variable.
61
62       An attempt to set an unrecognized attribute is a fatal error.  (The
63       error is trappable, but it still stops the compilation within that
64       "eval".)  Setting an attribute with a name that's all lowercase letters
65       that's not a built-in attribute (such as "foo") will result in a
66       warning with -w or "use warnings 'reserved'".
67
68   What "import" does
69       In the description it is mentioned that
70
71         sub foo : method;
72
73       is equivalent to
74
75         use attributes __PACKAGE__, \&foo, 'method';
76
77       As you might know this calls the "import" function of "attributes" at
78       compile time with these parameters: 'attributes', the caller's package
79       name, the reference to the code and 'method'.
80
81         attributes->import( __PACKAGE__, \&foo, 'method' );
82
83       So you want to know what "import" actually does?
84
85       First of all "import" gets the type of the third parameter ('CODE' in
86       this case).  "attributes.pm" checks if there is a subroutine called
87       "MODIFY_<reftype>_ATTRIBUTES" in the caller's namespace (here: 'main').
88       In this case a subroutine "MODIFY_CODE_ATTRIBUTES" is required.  Then
89       this method is called to check if you have used a "bad attribute".  The
90       subroutine call in this example would look like
91
92         MODIFY_CODE_ATTRIBUTES( 'main', \&foo, 'method' );
93
94       "MODIFY_<reftype>_ATTRIBUTES" has to return a list of all "bad
95       attributes".  If there are any bad attributes "import" croaks.
96
97       (See "Package-specific Attribute Handling" below.)
98
99   Built-in Attributes
100       The following are the built-in attributes for subroutines:
101
102       lvalue
103           Indicates that the referenced subroutine is a valid lvalue and can
104           be assigned to.  The subroutine must return a modifiable value such
105           as a scalar variable, as described in perlsub.
106
107           This module allows one to set this attribute on a subroutine that
108           is already defined.  For Perl subroutines (XSUBs are fine), it may
109           or may not do what you want, depending on the code inside the
110           subroutine, with details subject to change in future Perl versions.
111           You may run into problems with lvalue context not being propagated
112           properly into the subroutine, or maybe even assertion failures.
113           For this reason, a warning is emitted if warnings are enabled.  In
114           other words, you should only do this if you really know what you
115           are doing.  You have been warned.
116
117       method
118           Indicates that the referenced subroutine is a method.  A subroutine
119           so marked will not trigger the "Ambiguous call resolved as
120           CORE::%s" warning.
121
122       locked
123           The "locked" attribute has no effect in 5.10.0 and later.  It was
124           used as part of the now-removed "Perl 5.005 threads".
125
126   Available Subroutines
127       The following subroutines are available for general use once this
128       module has been loaded:
129
130       get This routine expects a single parameter--a reference to a
131           subroutine or variable.  It returns a list of attributes, which may
132           be empty.  If passed invalid arguments, it uses die() (via
133           Carp::croak) to raise a fatal exception.  If it can find an
134           appropriate package name for a class method lookup, it will include
135           the results from a "FETCH_type_ATTRIBUTES" call in its return list,
136           as described in "Package-specific Attribute Handling" below.
137           Otherwise, only built-in attributes will be returned.
138
139       reftype
140           This routine expects a single parameter--a reference to a
141           subroutine or variable.  It returns the built-in type of the
142           referenced variable, ignoring any package into which it might have
143           been blessed.  This can be useful for determining the type value
144           which forms part of the method names described in "Package-specific
145           Attribute Handling" below.
146
147       Note that these routines are not exported by default.
148
149   Package-specific Attribute Handling
150       WARNING: the mechanisms described here are still experimental.  Do not
151       rely on the current implementation.  In particular, there is no
152       provision for applying package attributes to 'cloned' copies of
153       subroutines used as closures.  (See "Making References" in perlref for
154       information on closures.)  Package-specific attribute handling may
155       change incompatibly in a future release.
156
157       When an attribute list is present in a declaration, a check is made to
158       see whether an attribute 'modify' handler is present in the appropriate
159       package (or its @ISA inheritance tree).  Similarly, when
160       "attributes::get" is called on a valid reference, a check is made for
161       an appropriate attribute 'fetch' handler.  See "EXAMPLES" to see how
162       the "appropriate package" determination works.
163
164       The handler names are based on the underlying type of the variable
165       being declared or of the reference passed.  Because these attributes
166       are associated with subroutine or variable declarations, this
167       deliberately ignores any possibility of being blessed into some
168       package.  Thus, a subroutine declaration uses "CODE" as its type, and
169       even a blessed hash reference uses "HASH" as its type.
170
171       The class methods invoked for modifying and fetching are these:
172
173       FETCH_type_ATTRIBUTES
174           This method is called with two arguments:  the relevant package
175           name, and a reference to a variable or subroutine for which
176           package-defined attributes are desired.  The expected return value
177           is a list of associated attributes.  This list may be empty.
178
179       MODIFY_type_ATTRIBUTES
180           This method is called with two fixed arguments, followed by the
181           list of attributes from the relevant declaration.  The two fixed
182           arguments are the relevant package name and a reference to the
183           declared subroutine or variable.  The expected return value is a
184           list of attributes which were not recognized by this handler.  Note
185           that this allows for a derived class to delegate a call to its base
186           class, and then only examine the attributes which the base class
187           didn't already handle for it.
188
189           The call to this method is currently made during the processing of
190           the declaration.  In particular, this means that a subroutine
191           reference will probably be for an undefined subroutine, even if
192           this declaration is actually part of the definition.
193
194       Calling "attributes::get()" from within the scope of a null package
195       declaration "package ;" for an unblessed variable reference will not
196       provide any starting package name for the 'fetch' method lookup.  Thus,
197       this circumstance will not result in a method call for package-defined
198       attributes.  A named subroutine knows to which symbol table entry it
199       belongs (or originally belonged), and it will use the corresponding
200       package.  An anonymous subroutine knows the package name into which it
201       was compiled (unless it was also compiled with a null package
202       declaration), and so it will use that package name.
203
204   Syntax of Attribute Lists
205       An attribute list is a sequence of attribute specifications, separated
206       by whitespace or a colon (with optional whitespace).  Each attribute
207       specification is a simple name, optionally followed by a parenthesised
208       parameter list.  If such a parameter list is present, it is scanned
209       past as for the rules for the "q()" operator.  (See "Quote and Quote-
210       like Operators" in perlop.)  The parameter list is passed as it was
211       found, however, and not as per "q()".
212
213       Some examples of syntactically valid attribute lists:
214
215           switch(10,foo(7,3))  :  expensive
216           Ugly('\(") :Bad
217           _5x5
218           lvalue method
219
220       Some examples of syntactically invalid attribute lists (with
221       annotation):
222
223           switch(10,foo()             # ()-string not balanced
224           Ugly('(')                   # ()-string not balanced
225           5x5                         # "5x5" not a valid identifier
226           Y2::north                   # "Y2::north" not a simple identifier
227           foo + bar                   # "+" neither a colon nor whitespace
228

EXPORTS

230   Default exports
231       None.
232
233   Available exports
234       The routines "get" and "reftype" are exportable.
235
236   Export tags defined
237       The ":ALL" tag will get all of the above exports.
238

EXAMPLES

240       Here are some samples of syntactically valid declarations, with
241       annotation as to how they resolve internally into "use attributes"
242       invocations by perl.  These examples are primarily useful to see how
243       the "appropriate package" is found for the possible method lookups for
244       package-defined attributes.
245
246       1.  Code:
247
248               package Canine;
249               package Dog;
250               my Canine $spot : Watchful ;
251
252           Effect:
253
254               use attributes ();
255               attributes::->import(Canine => \$spot, "Watchful");
256
257       2.  Code:
258
259               package Felis;
260               my $cat : Nervous;
261
262           Effect:
263
264               use attributes ();
265               attributes::->import(Felis => \$cat, "Nervous");
266
267       3.  Code:
268
269               package X;
270               sub foo : lvalue ;
271
272           Effect:
273
274               use attributes X => \&foo, "lvalue";
275
276       4.  Code:
277
278               package X;
279               sub Y::x : lvalue { 1 }
280
281           Effect:
282
283               use attributes Y => \&Y::x, "lvalue";
284
285       5.  Code:
286
287               package X;
288               sub foo { 1 }
289
290               package Y;
291               BEGIN { *bar = \&X::foo; }
292
293               package Z;
294               sub Y::bar : lvalue ;
295
296           Effect:
297
298               use attributes X => \&X::foo, "lvalue";
299
300       This last example is purely for purposes of completeness.  You should
301       not be trying to mess with the attributes of something in a package
302       that's not your own.
303

MORE EXAMPLES

305       1.
306               sub MODIFY_CODE_ATTRIBUTES {
307                  my ($class,$code,@attrs) = @_;
308
309                  my $allowed = 'MyAttribute';
310                  my @bad = grep { $_ ne $allowed } @attrs;
311
312                  return @bad;
313               }
314
315               sub foo : MyAttribute {
316                  print "foo\n";
317               }
318
319           This example runs.  At compile time "MODIFY_CODE_ATTRIBUTES" is
320           called.  In that subroutine, we check if any attribute is
321           disallowed and we return a list of these "bad attributes".
322
323           As we return an empty list, everything is fine.
324
325       2.
326             sub MODIFY_CODE_ATTRIBUTES {
327                my ($class,$code,@attrs) = @_;
328
329                my $allowed = 'MyAttribute';
330                my @bad = grep{ $_ ne $allowed }@attrs;
331
332                return @bad;
333             }
334
335             sub foo : MyAttribute Test {
336                print "foo\n";
337             }
338
339           This example is aborted at compile time as we use the attribute
340           "Test" which isn't allowed.  "MODIFY_CODE_ATTRIBUTES" returns a
341           list that contains a single element ('Test').
342

SEE ALSO

344       "Private Variables via my()" in perlsub and "Subroutine Attributes" in
345       perlsub for details on the basic declarations; "use" in perlfunc for
346       details on the normal invocation mechanism.
347
348
349
350perl v5.16.3                      2013-03-04                   attributes(3pm)
Impressum