summaryrefslogtreecommitdiffstats
path: root/upstream/debian-unstable/man3/attributes.3perl
blob: 24d01fbfc803d18abcec472d26fa44f9806ae45f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
.\" -*- mode: troff; coding: utf-8 -*-
.\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
.ie n \{\
.    ds C` ""
.    ds C' ""
'br\}
.el\{\
.    ds C`
.    ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\"
.\" If the F register is >0, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD.  Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{\
.    if \nF \{\
.        de IX
.        tm Index:\\$1\t\\n%\t"\\$2"
..
.        if !\nF==2 \{\
.            nr % 0
.            nr F 2
.        \}
.    \}
.\}
.rr rF
.\" ========================================================================
.\"
.IX Title "attributes 3perl"
.TH attributes 3perl 2024-05-30 "perl v5.38.2" "Perl Programmers Reference Guide"
.\" For nroff, turn off justification.  Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH NAME
attributes \- get/set subroutine or variable attributes
.SH SYNOPSIS
.IX Header "SYNOPSIS"
.Vb 3
\&  sub foo : method ;
\&  my ($x,@y,%z) : Bent = 1;
\&  my $s = sub : method { ... };
\&
\&  use attributes ();    # optional, to get subroutine declarations
\&  my @attrlist = attributes::get(\e&foo);
\&
\&  use attributes \*(Aqget\*(Aq; # import the attributes::get subroutine
\&  my @attrlist = get \e&foo;
.Ve
.SH DESCRIPTION
.IX Header "DESCRIPTION"
Subroutine declarations and definitions may optionally have attribute lists
associated with them.  (Variable \f(CW\*(C`my\*(C'\fR declarations also may, but see the
warning below.)  Perl handles these declarations by passing some information
about the call site and the thing being declared along with the attribute
list to this module.  In particular, the first example above is equivalent to
the following:
.PP
.Vb 1
\&    use attributes _\|_PACKAGE_\|_, \e&foo, \*(Aqmethod\*(Aq;
.Ve
.PP
The second example in the synopsis does something equivalent to this:
.PP
.Vb 6
\&    use attributes ();
\&    my ($x,@y,%z);
\&    attributes::\->import(_\|_PACKAGE_\|_, \e$x, \*(AqBent\*(Aq);
\&    attributes::\->import(_\|_PACKAGE_\|_, \e@y, \*(AqBent\*(Aq);
\&    attributes::\->import(_\|_PACKAGE_\|_, \e%z, \*(AqBent\*(Aq);
\&    ($x,@y,%z) = 1;
.Ve
.PP
Yes, that's a lot of expansion.
.PP
\&\fBWARNING\fR: attribute declarations for variables are still evolving.
The semantics and interfaces of such declarations could change in
future versions.  They are present for purposes of experimentation
with what the semantics ought to be.  Do not rely on the current
implementation of this feature.
.PP
There are only a few attributes currently handled by Perl itself (or
directly by this module, depending on how you look at it.)  However,
package-specific attributes are allowed by an extension mechanism.
(See "Package-specific Attribute Handling" below.)
.PP
The setting of subroutine attributes happens at compile time.
Variable attributes in \f(CW\*(C`our\*(C'\fR declarations are also applied at compile time.
However, \f(CW\*(C`my\*(C'\fR variables get their attributes applied at run-time.
This means that you have to \fIreach\fR the run-time component of the \f(CW\*(C`my\*(C'\fR
before those attributes will get applied.  For example:
.PP
.Vb 1
\&    my $x : Bent = 42 if 0;
.Ve
.PP
will neither assign 42 to \f(CW$x\fR \fInor\fR will it apply the \f(CW\*(C`Bent\*(C'\fR attribute
to the variable.
.PP
An attempt to set an unrecognized attribute is a fatal error.  (The
error is trappable, but it still stops the compilation within that
\&\f(CW\*(C`eval\*(C'\fR.)  Setting an attribute with a name that's all lowercase
letters that's not a built-in attribute (such as "foo") will result in
a warning with \fB\-w\fR or \f(CW\*(C`use warnings \*(Aqreserved\*(Aq\*(C'\fR.
.ie n .SS "What ""import"" does"
.el .SS "What \f(CWimport\fP does"
.IX Subsection "What import does"
In the description it is mentioned that
.PP
.Vb 1
\&  sub foo : method;
.Ve
.PP
is equivalent to
.PP
.Vb 1
\&  use attributes _\|_PACKAGE_\|_, \e&foo, \*(Aqmethod\*(Aq;
.Ve
.PP
As you might know this calls the \f(CW\*(C`import\*(C'\fR function of \f(CW\*(C`attributes\*(C'\fR at compile 
time with these parameters: 'attributes', the caller's package name, the reference 
to the code and 'method'.
.PP
.Vb 1
\&  attributes\->import( _\|_PACKAGE_\|_, \e&foo, \*(Aqmethod\*(Aq );
.Ve
.PP
So you want to know what \f(CW\*(C`import\*(C'\fR actually does?
.PP
First of all \f(CW\*(C`import\*(C'\fR gets the type of the third parameter ('CODE' in this case).
\&\f(CW\*(C`attributes.pm\*(C'\fR checks if there is a subroutine called \f(CW\*(C`MODIFY_<reftype>_ATTRIBUTES\*(C'\fR
in the caller's namespace (here: 'main').  In this case a
subroutine \f(CW\*(C`MODIFY_CODE_ATTRIBUTES\*(C'\fR is required.  Then this
method is called to check if you have used a "bad attribute".
The subroutine call in this example would look like
.PP
.Vb 1
\&  MODIFY_CODE_ATTRIBUTES( \*(Aqmain\*(Aq, \e&foo, \*(Aqmethod\*(Aq );
.Ve
.PP
\&\f(CW\*(C`MODIFY_<reftype>_ATTRIBUTES\*(C'\fR has to return a list of all "bad attributes".
If there are any bad attributes \f(CW\*(C`import\*(C'\fR croaks.
.PP
(See "Package-specific Attribute Handling" below.)
.SS "Built-in Attributes"
.IX Subsection "Built-in Attributes"
The following are the built-in attributes for subroutines:
.IP lvalue 4
.IX Item "lvalue"
Indicates that the referenced subroutine is a valid lvalue and can
be assigned to.  The subroutine must return a modifiable value such
as a scalar variable, as described in perlsub.
.Sp
This module allows one to set this attribute on a subroutine that is
already defined.  For Perl subroutines (XSUBs are fine), it may or may not
do what you want, depending on the code inside the subroutine, with details
subject to change in future Perl versions.  You may run into problems with
lvalue context not being propagated properly into the subroutine, or maybe
even assertion failures.  For this reason, a warning is emitted if warnings
are enabled.  In other words, you should only do this if you really know
what you are doing.  You have been warned.
.IP method 4
.IX Item "method"
Indicates that the referenced subroutine
is a method.  A subroutine so marked
will not trigger the "Ambiguous call resolved as CORE::%s" warning.
.IP prototype(..) 4
.IX Item "prototype(..)"
The "prototype" attribute is an alternate means of specifying a prototype
on a sub.  The desired prototype is within the parens.
.Sp
The prototype from the attribute is assigned to the sub immediately after
the prototype from the sub, which means that if both are declared at the
same time, the traditionally defined prototype is ignored.  In other words,
\&\f(CW\*(C`sub foo($$) : prototype(@) {}\*(C'\fR is indistinguishable from \f(CW\*(C`sub foo(@){}\*(C'\fR.
.Sp
If illegalproto warnings are enabled, the prototype declared inside this
attribute will be sanity checked at compile time.
.IP const 4
.IX Item "const"
This experimental attribute, introduced in Perl 5.22, only applies to
anonymous subroutines.  It causes the subroutine to be called as soon as
the \f(CW\*(C`sub\*(C'\fR expression is evaluated.  The return value is captured and
turned into a constant subroutine.
.PP
The following are the built-in attributes for variables:
.IP shared 4
.IX Item "shared"
Indicates that the referenced variable can be shared across different threads
when used in conjunction with the threads and threads::shared modules.
.SS "Available Subroutines"
.IX Subsection "Available Subroutines"
The following subroutines are available for general use once this module
has been loaded:
.IP get 4
.IX Item "get"
This routine expects a single parameter\-\-a reference to a
subroutine or variable.  It returns a list of attributes, which may be
empty.  If passed invalid arguments, it uses \fBdie()\fR (via Carp::croak)
to raise a fatal exception.  If it can find an appropriate package name
for a class method lookup, it will include the results from a
\&\f(CW\*(C`FETCH_\fR\f(CItype\fR\f(CW_ATTRIBUTES\*(C'\fR call in its return list, as described in
"Package-specific Attribute Handling" below.
Otherwise, only built-in attributes will be returned.
.IP reftype 4
.IX Item "reftype"
This routine expects a single parameter\-\-a reference to a subroutine or
variable.  It returns the built-in type of the referenced variable,
ignoring any package into which it might have been blessed.
This can be useful for determining the \fItype\fR value which forms part of
the method names described in "Package-specific Attribute Handling" below.
.PP
Note that these routines are \fInot\fR exported by default.
.SS "Package-specific Attribute Handling"
.IX Subsection "Package-specific Attribute Handling"
\&\fBWARNING\fR: the mechanisms described here are still experimental.  Do not
rely on the current implementation.  In particular, there is no provision
for applying package attributes to 'cloned' copies of subroutines used as
closures.  (See "Making References" in perlref for information on closures.)
Package-specific attribute handling may change incompatibly in a future
release.
.PP
When an attribute list is present in a declaration, a check is made to see
whether an attribute 'modify' handler is present in the appropriate package
(or its \f(CW@ISA\fR inheritance tree).  Similarly, when \f(CW\*(C`attributes::get\*(C'\fR is
called on a valid reference, a check is made for an appropriate attribute
\&'fetch' handler.  See "EXAMPLES" to see how the "appropriate package"
determination works.
.PP
The handler names are based on the underlying type of the variable being
declared or of the reference passed.  Because these attributes are
associated with subroutine or variable declarations, this deliberately
ignores any possibility of being blessed into some package.  Thus, a
subroutine declaration uses "CODE" as its \fItype\fR, and even a blessed
hash reference uses "HASH" as its \fItype\fR.
.PP
The class methods invoked for modifying and fetching are these:
.IP FETCH_\fItype\fR_ATTRIBUTES 4
.IX Item "FETCH_type_ATTRIBUTES"
This method is called with two arguments:  the relevant package name,
and a reference to a variable or subroutine for which package-defined
attributes are desired.  The expected return value is a list of
associated attributes.  This list may be empty.
.IP MODIFY_\fItype\fR_ATTRIBUTES 4
.IX Item "MODIFY_type_ATTRIBUTES"
This method is called with two fixed arguments, followed by the list of
attributes from the relevant declaration.  The two fixed arguments are
the relevant package name and a reference to the declared subroutine or
variable.  The expected return value is a list of attributes which were
not recognized by this handler.  Note that this allows for a derived class
to delegate a call to its base class, and then only examine the attributes
which the base class didn't already handle for it.
.Sp
The call to this method is currently made \fIduring\fR the processing of the
declaration.  In particular, this means that a subroutine reference will
probably be for an undefined subroutine, even if this declaration is
actually part of the definition.
.PP
Calling \f(CWattributes::get()\fR from within the scope of a null package
declaration \f(CW\*(C`package ;\*(C'\fR for an unblessed variable reference will
not provide any starting package name for the 'fetch' method lookup.
Thus, this circumstance will not result in a method call for package-defined
attributes.  A named subroutine knows to which symbol table entry it belongs
(or originally belonged), and it will use the corresponding package.
An anonymous subroutine knows the package name into which it was compiled
(unless it was also compiled with a null package declaration), and so it
will use that package name.
.SS "Syntax of Attribute Lists"
.IX Subsection "Syntax of Attribute Lists"
An attribute list is a sequence of attribute specifications, separated by
whitespace or a colon (with optional whitespace).
Each attribute specification is a simple
name, optionally followed by a parenthesised parameter list.
If such a parameter list is present, it is scanned past as for the rules
for the \f(CWq()\fR operator.  (See "Quote and Quote-like Operators" in perlop.)
The parameter list is passed as it was found, however, and not as per \f(CWq()\fR.
.PP
Some examples of syntactically valid attribute lists:
.PP
.Vb 4
\&    switch(10,foo(7,3))  :  expensive
\&    Ugly(\*(Aq\e(") :Bad
\&    _5x5
\&    lvalue method
.Ve
.PP
Some examples of syntactically invalid attribute lists (with annotation):
.PP
.Vb 5
\&    switch(10,foo()             # ()\-string not balanced
\&    Ugly(\*(Aq(\*(Aq)                   # ()\-string not balanced
\&    5x5                         # "5x5" not a valid identifier
\&    Y2::north                   # "Y2::north" not a simple identifier
\&    foo + bar                   # "+" neither a colon nor whitespace
.Ve
.SH EXPORTS
.IX Header "EXPORTS"
.SS "Default exports"
.IX Subsection "Default exports"
None.
.SS "Available exports"
.IX Subsection "Available exports"
The routines \f(CW\*(C`get\*(C'\fR and \f(CW\*(C`reftype\*(C'\fR are exportable.
.SS "Export tags defined"
.IX Subsection "Export tags defined"
The \f(CW\*(C`:ALL\*(C'\fR tag will get all of the above exports.
.SH EXAMPLES
.IX Header "EXAMPLES"
Here are some samples of syntactically valid declarations, with annotation
as to how they resolve internally into \f(CW\*(C`use attributes\*(C'\fR invocations by
perl.  These examples are primarily useful to see how the "appropriate
package" is found for the possible method lookups for package-defined
attributes.
.IP 1. 4
Code:
.Sp
.Vb 3
\&    package Canine;
\&    package Dog;
\&    my Canine $spot : Watchful ;
.Ve
.Sp
Effect:
.Sp
.Vb 2
\&    use attributes ();
\&    attributes::\->import(Canine => \e$spot, "Watchful");
.Ve
.IP 2. 4
Code:
.Sp
.Vb 2
\&    package Felis;
\&    my $cat : Nervous;
.Ve
.Sp
Effect:
.Sp
.Vb 2
\&    use attributes ();
\&    attributes::\->import(Felis => \e$cat, "Nervous");
.Ve
.IP 3. 4
Code:
.Sp
.Vb 2
\&    package X;
\&    sub foo : lvalue ;
.Ve
.Sp
Effect:
.Sp
.Vb 1
\&    use attributes X => \e&foo, "lvalue";
.Ve
.IP 4. 4
Code:
.Sp
.Vb 2
\&    package X;
\&    sub Y::x : lvalue { 1 }
.Ve
.Sp
Effect:
.Sp
.Vb 1
\&    use attributes Y => \e&Y::x, "lvalue";
.Ve
.IP 5. 4
Code:
.Sp
.Vb 2
\&    package X;
\&    sub foo { 1 }
\&
\&    package Y;
\&    BEGIN { *bar = \e&X::foo; }
\&
\&    package Z;
\&    sub Y::bar : lvalue ;
.Ve
.Sp
Effect:
.Sp
.Vb 1
\&    use attributes X => \e&X::foo, "lvalue";
.Ve
.PP
This last example is purely for purposes of completeness.  You should not
be trying to mess with the attributes of something in a package that's
not your own.
.SH "MORE EXAMPLES"
.IX Header "MORE EXAMPLES"
.IP 1. 4
.Vb 2
\&    sub MODIFY_CODE_ATTRIBUTES {
\&       my ($class,$code,@attrs) = @_;
\&
\&       my $allowed = \*(AqMyAttribute\*(Aq;
\&       my @bad = grep { $_ ne $allowed } @attrs;
\&
\&       return @bad;
\&    }
\&
\&    sub foo : MyAttribute {
\&       print "foo\en";
\&    }
.Ve
.Sp
This example runs.  At compile time
\&\f(CW\*(C`MODIFY_CODE_ATTRIBUTES\*(C'\fR is called.  In that
subroutine, we check if any attribute is disallowed and we return a list of
these "bad attributes".
.Sp
As we return an empty list, everything is fine.
.IP 2. 4
.Vb 2
\&  sub MODIFY_CODE_ATTRIBUTES {
\&     my ($class,$code,@attrs) = @_;
\&
\&     my $allowed = \*(AqMyAttribute\*(Aq;
\&     my @bad = grep{ $_ ne $allowed }@attrs;
\&
\&     return @bad;
\&  }
\&
\&  sub foo : MyAttribute Test {
\&     print "foo\en";
\&  }
.Ve
.Sp
This example is aborted at compile time as we use the attribute "Test" which
isn't allowed.  \f(CW\*(C`MODIFY_CODE_ATTRIBUTES\*(C'\fR
returns a list that contains a single
element ('Test').
.SH "SEE ALSO"
.IX Header "SEE ALSO"
"Private Variables via \fBmy()\fR" in perlsub and
"Subroutine Attributes" in perlsub for details on the basic declarations;
"use" in perlfunc for details on the normal invocation mechanism.