1attributes(3pm) Perl Programmers Reference Guide attributes(3pm)
2
3
4
6 attributes - get/set subroutine or variable attributes
7
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
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 exam‐
25 ple 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 warn‐
66 ing with -w or "use warnings 'reserved'".
67
68 Built-in Attributes
69
70 The following are the built-in attributes for subroutines:
71
72 locked
73 5.005 threads only! The use of the "locked" attribute currently
74 only makes sense if you are using the deprecated "Perl 5.005
75 threads" implementation of threads.
76
77 Setting this attribute is only meaningful when the subroutine or
78 method is to be called by multiple threads. When set on a method
79 subroutine (i.e., one marked with the method attribute below), Perl
80 ensures that any invocation of it implicitly locks its first argu‐
81 ment before execution. When set on a non-method subroutine, Perl
82 ensures that a lock is taken on the subroutine itself before execu‐
83 tion. The semantics of the lock are exactly those of one explic‐
84 itly taken with the "lock" operator immediately after the subrou‐
85 tine is entered.
86
87 method
88 Indicates that the referenced subroutine is a method. This has a
89 meaning when taken together with the locked attribute, as described
90 there. It also means that a subroutine so marked will not trigger
91 the "Ambiguous call resolved as CORE::%s" warning.
92
93 lvalue
94 Indicates that the referenced subroutine is a valid lvalue and can
95 be assigned to. The subroutine must return a modifiable value such
96 as a scalar variable, as described in perlsub.
97
98 For global variables there is "unique" attribute: see "our" in perl‐
99 func.
100
101 Available Subroutines
102
103 The following subroutines are available for general use once this mod‐
104 ule has been loaded:
105
106 get This routine expects a single parameter--a reference to a subrou‐
107 tine or variable. It returns a list of attributes, which may be
108 empty. If passed invalid arguments, it uses die() (via
109 Carp::croak) to raise a fatal exception. If it can find an appro‐
110 priate package name for a class method lookup, it will include the
111 results from a "FETCH_type_ATTRIBUTES" call in its return list, as
112 described in "Package-specific Attribute Handling" below. Other‐
113 wise, only built-in attributes will be returned.
114
115 reftype
116 This routine expects a single parameter--a reference to a subrou‐
117 tine or variable. It returns the built-in type of the referenced
118 variable, ignoring any package into which it might have been
119 blessed. This can be useful for determining the type value which
120 forms part of the method names described in "Package-specific
121 Attribute Handling" below.
122
123 Note that these routines are not exported by default.
124
125 Package-specific Attribute Handling
126
127 WARNING: the mechanisms described here are still experimental. Do not
128 rely on the current implementation. In particular, there is no provi‐
129 sion for applying package attributes to 'cloned' copies of subroutines
130 used as closures. (See "Making References" in perlref for information
131 on closures.) Package-specific attribute handling may change incompat‐
132 ibly in a future release.
133
134 When an attribute list is present in a declaration, a check is made to
135 see whether an attribute 'modify' handler is present in the appropriate
136 package (or its @ISA inheritance tree). Similarly, when
137 "attributes::get" is called on a valid reference, a check is made for
138 an appropriate attribute 'fetch' handler. See "EXAMPLES" to see how
139 the "appropriate package" determination works.
140
141 The handler names are based on the underlying type of the variable
142 being declared or of the reference passed. Because these attributes
143 are associated with subroutine or variable declarations, this deliber‐
144 ately ignores any possibility of being blessed into some package.
145 Thus, a subroutine declaration uses "CODE" as its type, and even a
146 blessed hash reference uses "HASH" as its type.
147
148 The class methods invoked for modifying and fetching are these:
149
150 FETCH_type_ATTRIBUTES
151 This method receives a single argument, which is a reference to the
152 variable or subroutine for which package-defined attributes are
153 desired. The expected return value is a list of associated
154 attributes. This list may be empty.
155
156 MODIFY_type_ATTRIBUTES
157 This method is called with two fixed arguments, followed by the
158 list of attributes from the relevant declaration. The two fixed
159 arguments are the relevant package name and a reference to the
160 declared subroutine or variable. The expected return value is a
161 list of attributes which were not recognized by this handler. Note
162 that this allows for a derived class to delegate a call to its base
163 class, and then only examine the attributes which the base class
164 didn't already handle for it.
165
166 The call to this method is currently made during the processing of
167 the declaration. In particular, this means that a subroutine ref‐
168 erence will probably be for an undefined subroutine, even if this
169 declaration is actually part of the definition.
170
171 Calling "attributes::get()" from within the scope of a null package
172 declaration "package ;" for an unblessed variable reference will not
173 provide any starting package name for the 'fetch' method lookup. Thus,
174 this circumstance will not result in a method call for package-defined
175 attributes. A named subroutine knows to which symbol table entry it
176 belongs (or originally belonged), and it will use the corresponding
177 package. An anonymous subroutine knows the package name into which it
178 was compiled (unless it was also compiled with a null package declara‐
179 tion), and so it will use that package name.
180
181 Syntax of Attribute Lists
182
183 An attribute list is a sequence of attribute specifications, separated
184 by whitespace or a colon (with optional whitespace). Each attribute
185 specification is a simple name, optionally followed by a parenthesised
186 parameter list. If such a parameter list is present, it is scanned
187 past as for the rules for the "q()" operator. (See "Quote and Quote-
188 like Operators" in perlop.) The parameter list is passed as it was
189 found, however, and not as per "q()".
190
191 Some examples of syntactically valid attribute lists:
192
193 switch(10,foo(7,3)) : expensive
194 Ugly('\(") :Bad
195 _5x5
196 locked method
197
198 Some examples of syntactically invalid attribute lists (with annota‐
199 tion):
200
201 switch(10,foo() # ()-string not balanced
202 Ugly('(') # ()-string not balanced
203 5x5 # "5x5" not a valid identifier
204 Y2::north # "Y2::north" not a simple identifier
205 foo + bar # "+" neither a colon nor whitespace
206
208 Default exports
209
210 None.
211
212 Available exports
213
214 The routines "get" and "reftype" are exportable.
215
216 Export tags defined
217
218 The ":ALL" tag will get all of the above exports.
219
221 Here are some samples of syntactically valid declarations, with annota‐
222 tion as to how they resolve internally into "use attributes" invoca‐
223 tions by perl. These examples are primarily useful to see how the
224 "appropriate package" is found for the possible method lookups for
225 package-defined attributes.
226
227 1. Code:
228
229 package Canine;
230 package Dog;
231 my Canine $spot : Watchful ;
232
233 Effect:
234
235 use attributes ();
236 attributes::->import(Canine => \$spot, "Watchful");
237
238 2. Code:
239
240 package Felis;
241 my $cat : Nervous;
242
243 Effect:
244
245 use attributes ();
246 attributes::->import(Felis => \$cat, "Nervous");
247
248 3. Code:
249
250 package X;
251 sub foo : locked ;
252
253 Effect:
254
255 use attributes X => \&foo, "locked";
256
257 4. Code:
258
259 package X;
260 sub Y::x : locked { 1 }
261
262 Effect:
263
264 use attributes Y => \&Y::x, "locked";
265
266 5. Code:
267
268 package X;
269 sub foo { 1 }
270
271 package Y;
272 BEGIN { *bar = \&X::foo; }
273
274 package Z;
275 sub Y::bar : locked ;
276
277 Effect:
278
279 use attributes X => \&X::foo, "locked";
280
281 This last example is purely for purposes of completeness. You should
282 not be trying to mess with the attributes of something in a package
283 that's not your own.
284
286 "Private Variables via my()" in perlsub and "Subroutine Attributes" in
287 perlsub for details on the basic declarations; attrs for the obsoles‐
288 cent form of subroutine attribute specification which this module
289 replaces; "use" in perlfunc for details on the normal invocation mecha‐
290 nism.
291
292
293
294perl v5.8.8 2001-09-21 attributes(3pm)