| 1 | .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32 |
| 2 | .\" |
| 3 | .\" Standard preamble: |
| 4 | .\" ======================================================================== |
| 5 | .de Sh \" Subsection heading |
| 6 | .br |
| 7 | .if t .Sp |
| 8 | .ne 5 |
| 9 | .PP |
| 10 | \fB\\$1\fR |
| 11 | .PP |
| 12 | .. |
| 13 | .de Sp \" Vertical space (when we can't use .PP) |
| 14 | .if t .sp .5v |
| 15 | .if n .sp |
| 16 | .. |
| 17 | .de Vb \" Begin verbatim text |
| 18 | .ft CW |
| 19 | .nf |
| 20 | .ne \\$1 |
| 21 | .. |
| 22 | .de Ve \" End verbatim text |
| 23 | .ft R |
| 24 | .fi |
| 25 | .. |
| 26 | .\" Set up some character translations and predefined strings. \*(-- will |
| 27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left |
| 28 | .\" double quote, and \*(R" will give a right double quote. | will give a |
| 29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to |
| 30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' |
| 31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. |
| 32 | .tr \(*W-|\(bv\*(Tr |
| 33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' |
| 34 | .ie n \{\ |
| 35 | . ds -- \(*W- |
| 36 | . ds PI pi |
| 37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch |
| 38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch |
| 39 | . ds L" "" |
| 40 | . ds R" "" |
| 41 | . ds C` "" |
| 42 | . ds C' "" |
| 43 | 'br\} |
| 44 | .el\{\ |
| 45 | . ds -- \|\(em\| |
| 46 | . ds PI \(*p |
| 47 | . ds L" `` |
| 48 | . ds R" '' |
| 49 | 'br\} |
| 50 | .\" |
| 51 | .\" If the F register is turned on, we'll generate index entries on stderr for |
| 52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index |
| 53 | .\" entries marked with X<> in POD. Of course, you'll have to process the |
| 54 | .\" output yourself in some meaningful fashion. |
| 55 | .if \nF \{\ |
| 56 | . de IX |
| 57 | . tm Index:\\$1\t\\n%\t"\\$2" |
| 58 | .. |
| 59 | . nr % 0 |
| 60 | . rr F |
| 61 | .\} |
| 62 | .\" |
| 63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 64 | .\" way too many mistakes in technical documents. |
| 65 | .hy 0 |
| 66 | .if n .na |
| 67 | .\" |
| 68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). |
| 69 | .\" Fear. Run. Save yourself. No user-serviceable parts. |
| 70 | . \" fudge factors for nroff and troff |
| 71 | .if n \{\ |
| 72 | . ds #H 0 |
| 73 | . ds #V .8m |
| 74 | . ds #F .3m |
| 75 | . ds #[ \f1 |
| 76 | . ds #] \fP |
| 77 | .\} |
| 78 | .if t \{\ |
| 79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) |
| 80 | . ds #V .6m |
| 81 | . ds #F 0 |
| 82 | . ds #[ \& |
| 83 | . ds #] \& |
| 84 | .\} |
| 85 | . \" simple accents for nroff and troff |
| 86 | .if n \{\ |
| 87 | . ds ' \& |
| 88 | . ds ` \& |
| 89 | . ds ^ \& |
| 90 | . ds , \& |
| 91 | . ds ~ ~ |
| 92 | . ds / |
| 93 | .\} |
| 94 | .if t \{\ |
| 95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" |
| 96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' |
| 97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' |
| 98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' |
| 99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' |
| 100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' |
| 101 | .\} |
| 102 | . \" troff and (daisy-wheel) nroff accents |
| 103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' |
| 104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' |
| 105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] |
| 106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' |
| 107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' |
| 108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] |
| 109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] |
| 110 | .ds ae a\h'-(\w'a'u*4/10)'e |
| 111 | .ds Ae A\h'-(\w'A'u*4/10)'E |
| 112 | . \" corrections for vroff |
| 113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' |
| 114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' |
| 115 | . \" for low resolution devices (crt and lpr) |
| 116 | .if \n(.H>23 .if \n(.V>19 \ |
| 117 | \{\ |
| 118 | . ds : e |
| 119 | . ds 8 ss |
| 120 | . ds o a |
| 121 | . ds d- d\h'-1'\(ga |
| 122 | . ds D- D\h'-1'\(hy |
| 123 | . ds th \o'bp' |
| 124 | . ds Th \o'LP' |
| 125 | . ds ae ae |
| 126 | . ds Ae AE |
| 127 | .\} |
| 128 | .rm #[ #] #H #V #F C |
| 129 | .\" ======================================================================== |
| 130 | .\" |
| 131 | .IX Title "attributes 3" |
| 132 | .TH attributes 3 "2001-09-21" "perl v5.8.8" "Perl Programmers Reference Guide" |
| 133 | .SH "NAME" |
| 134 | attributes \- get/set subroutine or variable attributes |
| 135 | .SH "SYNOPSIS" |
| 136 | .IX Header "SYNOPSIS" |
| 137 | .Vb 3 |
| 138 | \& sub foo : method ; |
| 139 | \& my ($x,@y,%z) : Bent = 1; |
| 140 | \& my $s = sub : method { ... }; |
| 141 | .Ve |
| 142 | .PP |
| 143 | .Vb 2 |
| 144 | \& use attributes (); # optional, to get subroutine declarations |
| 145 | \& my @attrlist = attributes::get(\e&foo); |
| 146 | .Ve |
| 147 | .PP |
| 148 | .Vb 2 |
| 149 | \& use attributes 'get'; # import the attributes::get subroutine |
| 150 | \& my @attrlist = get \e&foo; |
| 151 | .Ve |
| 152 | .SH "DESCRIPTION" |
| 153 | .IX Header "DESCRIPTION" |
| 154 | Subroutine declarations and definitions may optionally have attribute lists |
| 155 | associated with them. (Variable \f(CW\*(C`my\*(C'\fR declarations also may, but see the |
| 156 | warning below.) Perl handles these declarations by passing some information |
| 157 | about the call site and the thing being declared along with the attribute |
| 158 | list to this module. In particular, the first example above is equivalent to |
| 159 | the following: |
| 160 | .PP |
| 161 | .Vb 1 |
| 162 | \& use attributes __PACKAGE__, \e&foo, 'method'; |
| 163 | .Ve |
| 164 | .PP |
| 165 | The second example in the synopsis does something equivalent to this: |
| 166 | .PP |
| 167 | .Vb 6 |
| 168 | \& use attributes (); |
| 169 | \& my ($x,@y,%z); |
| 170 | \& attributes::->import(__PACKAGE__, \e$x, 'Bent'); |
| 171 | \& attributes::->import(__PACKAGE__, \e@y, 'Bent'); |
| 172 | \& attributes::->import(__PACKAGE__, \e%z, 'Bent'); |
| 173 | \& ($x,@y,%z) = 1; |
| 174 | .Ve |
| 175 | .PP |
| 176 | Yes, that's a lot of expansion. |
| 177 | .PP |
| 178 | \&\fB\s-1WARNING\s0\fR: attribute declarations for variables are still evolving. |
| 179 | The semantics and interfaces of such declarations could change in |
| 180 | future versions. They are present for purposes of experimentation |
| 181 | with what the semantics ought to be. Do not rely on the current |
| 182 | implementation of this feature. |
| 183 | .PP |
| 184 | There are only a few attributes currently handled by Perl itself (or |
| 185 | directly by this module, depending on how you look at it.) However, |
| 186 | package-specific attributes are allowed by an extension mechanism. |
| 187 | (See \*(L"Package\-specific Attribute Handling\*(R" below.) |
| 188 | .PP |
| 189 | The setting of subroutine attributes happens at compile time. |
| 190 | Variable attributes in \f(CW\*(C`our\*(C'\fR declarations are also applied at compile time. |
| 191 | However, \f(CW\*(C`my\*(C'\fR variables get their attributes applied at run\-time. |
| 192 | This means that you have to \fIreach\fR the run-time component of the \f(CW\*(C`my\*(C'\fR |
| 193 | before those attributes will get applied. For example: |
| 194 | .PP |
| 195 | .Vb 1 |
| 196 | \& my $x : Bent = 42 if 0; |
| 197 | .Ve |
| 198 | .PP |
| 199 | will neither assign 42 to \f(CW$x\fR \fInor\fR will it apply the \f(CW\*(C`Bent\*(C'\fR attribute |
| 200 | to the variable. |
| 201 | .PP |
| 202 | An attempt to set an unrecognized attribute is a fatal error. (The |
| 203 | error is trappable, but it still stops the compilation within that |
| 204 | \&\f(CW\*(C`eval\*(C'\fR.) Setting an attribute with a name that's all lowercase |
| 205 | letters that's not a built-in attribute (such as \*(L"foo\*(R") will result in |
| 206 | a warning with \fB\-w\fR or \f(CW\*(C`use warnings 'reserved'\*(C'\fR. |
| 207 | .Sh "Built-in Attributes" |
| 208 | .IX Subsection "Built-in Attributes" |
| 209 | The following are the built-in attributes for subroutines: |
| 210 | .IP "locked" 4 |
| 211 | .IX Item "locked" |
| 212 | \&\fB5.005 threads only! The use of the \*(L"locked\*(R" attribute currently |
| 213 | only makes sense if you are using the deprecated \*(L"Perl 5.005 threads\*(R" |
| 214 | implementation of threads.\fR |
| 215 | .Sp |
| 216 | Setting this attribute is only meaningful when the subroutine or |
| 217 | method is to be called by multiple threads. When set on a method |
| 218 | subroutine (i.e., one marked with the \fBmethod\fR attribute below), |
| 219 | Perl ensures that any invocation of it implicitly locks its first |
| 220 | argument before execution. When set on a non-method subroutine, |
| 221 | Perl ensures that a lock is taken on the subroutine itself before |
| 222 | execution. The semantics of the lock are exactly those of one |
| 223 | explicitly taken with the \f(CW\*(C`lock\*(C'\fR operator immediately after the |
| 224 | subroutine is entered. |
| 225 | .IP "method" 4 |
| 226 | .IX Item "method" |
| 227 | Indicates that the referenced subroutine is a method. |
| 228 | This has a meaning when taken together with the \fBlocked\fR attribute, |
| 229 | as described there. It also means that a subroutine so marked |
| 230 | will not trigger the \*(L"Ambiguous call resolved as CORE::%s\*(R" warning. |
| 231 | .IP "lvalue" 4 |
| 232 | .IX Item "lvalue" |
| 233 | Indicates that the referenced subroutine is a valid lvalue and can |
| 234 | be assigned to. The subroutine must return a modifiable value such |
| 235 | as a scalar variable, as described in perlsub. |
| 236 | .PP |
| 237 | For global variables there is \f(CW\*(C`unique\*(C'\fR attribute: see \*(L"our\*(R" in perlfunc. |
| 238 | .Sh "Available Subroutines" |
| 239 | .IX Subsection "Available Subroutines" |
| 240 | The following subroutines are available for general use once this module |
| 241 | has been loaded: |
| 242 | .IP "get" 4 |
| 243 | .IX Item "get" |
| 244 | This routine expects a single parameter\*(--a reference to a |
| 245 | subroutine or variable. It returns a list of attributes, which may be |
| 246 | empty. If passed invalid arguments, it uses \fIdie()\fR (via Carp::croak) |
| 247 | to raise a fatal exception. If it can find an appropriate package name |
| 248 | for a class method lookup, it will include the results from a |
| 249 | \&\f(CW\*(C`FETCH_\f(CItype\f(CW_ATTRIBUTES\*(C'\fR call in its return list, as described in |
| 250 | \&\*(L"Package\-specific Attribute Handling\*(R" below. |
| 251 | Otherwise, only built-in attributes will be returned. |
| 252 | .IP "reftype" 4 |
| 253 | .IX Item "reftype" |
| 254 | This routine expects a single parameter\*(--a reference to a subroutine or |
| 255 | variable. It returns the built-in type of the referenced variable, |
| 256 | ignoring any package into which it might have been blessed. |
| 257 | This can be useful for determining the \fItype\fR value which forms part of |
| 258 | the method names described in \*(L"Package\-specific Attribute Handling\*(R" below. |
| 259 | .PP |
| 260 | Note that these routines are \fInot\fR exported by default. |
| 261 | .Sh "Package-specific Attribute Handling" |
| 262 | .IX Subsection "Package-specific Attribute Handling" |
| 263 | \&\fB\s-1WARNING\s0\fR: the mechanisms described here are still experimental. Do not |
| 264 | rely on the current implementation. In particular, there is no provision |
| 265 | for applying package attributes to 'cloned' copies of subroutines used as |
| 266 | closures. (See \*(L"Making References\*(R" in perlref for information on closures.) |
| 267 | Package-specific attribute handling may change incompatibly in a future |
| 268 | release. |
| 269 | .PP |
| 270 | When an attribute list is present in a declaration, a check is made to see |
| 271 | whether an attribute 'modify' handler is present in the appropriate package |
| 272 | (or its \f(CW@ISA\fR inheritance tree). Similarly, when \f(CW\*(C`attributes::get\*(C'\fR is |
| 273 | called on a valid reference, a check is made for an appropriate attribute |
| 274 | \&'fetch' handler. See \*(L"\s-1EXAMPLES\s0\*(R" to see how the \*(L"appropriate package\*(R" |
| 275 | determination works. |
| 276 | .PP |
| 277 | The handler names are based on the underlying type of the variable being |
| 278 | declared or of the reference passed. Because these attributes are |
| 279 | associated with subroutine or variable declarations, this deliberately |
| 280 | ignores any possibility of being blessed into some package. Thus, a |
| 281 | subroutine declaration uses \*(L"\s-1CODE\s0\*(R" as its \fItype\fR, and even a blessed |
| 282 | hash reference uses \*(L"\s-1HASH\s0\*(R" as its \fItype\fR. |
| 283 | .PP |
| 284 | The class methods invoked for modifying and fetching are these: |
| 285 | .IP "\s-1FETCH_\s0\fItype\fR_ATTRIBUTES" 4 |
| 286 | .IX Item "FETCH_type_ATTRIBUTES" |
| 287 | This method receives a single argument, which is a reference to the |
| 288 | variable or subroutine for which package-defined attributes are desired. |
| 289 | The expected return value is a list of associated attributes. |
| 290 | This list may be empty. |
| 291 | .IP "\s-1MODIFY_\s0\fItype\fR_ATTRIBUTES" 4 |
| 292 | .IX Item "MODIFY_type_ATTRIBUTES" |
| 293 | This method is called with two fixed arguments, followed by the list of |
| 294 | attributes from the relevant declaration. The two fixed arguments are |
| 295 | the relevant package name and a reference to the declared subroutine or |
| 296 | variable. The expected return value is a list of attributes which were |
| 297 | not recognized by this handler. Note that this allows for a derived class |
| 298 | to delegate a call to its base class, and then only examine the attributes |
| 299 | which the base class didn't already handle for it. |
| 300 | .Sp |
| 301 | The call to this method is currently made \fIduring\fR the processing of the |
| 302 | declaration. In particular, this means that a subroutine reference will |
| 303 | probably be for an undefined subroutine, even if this declaration is |
| 304 | actually part of the definition. |
| 305 | .PP |
| 306 | Calling \f(CW\*(C`attributes::get()\*(C'\fR from within the scope of a null package |
| 307 | declaration \f(CW\*(C`package ;\*(C'\fR for an unblessed variable reference will |
| 308 | not provide any starting package name for the 'fetch' method lookup. |
| 309 | Thus, this circumstance will not result in a method call for package-defined |
| 310 | attributes. A named subroutine knows to which symbol table entry it belongs |
| 311 | (or originally belonged), and it will use the corresponding package. |
| 312 | An anonymous subroutine knows the package name into which it was compiled |
| 313 | (unless it was also compiled with a null package declaration), and so it |
| 314 | will use that package name. |
| 315 | .Sh "Syntax of Attribute Lists" |
| 316 | .IX Subsection "Syntax of Attribute Lists" |
| 317 | An attribute list is a sequence of attribute specifications, separated by |
| 318 | whitespace or a colon (with optional whitespace). |
| 319 | Each attribute specification is a simple |
| 320 | name, optionally followed by a parenthesised parameter list. |
| 321 | If such a parameter list is present, it is scanned past as for the rules |
| 322 | for the \f(CW\*(C`q()\*(C'\fR operator. (See \*(L"Quote and Quote-like Operators\*(R" in perlop.) |
| 323 | The parameter list is passed as it was found, however, and not as per \f(CW\*(C`q()\*(C'\fR. |
| 324 | .PP |
| 325 | Some examples of syntactically valid attribute lists: |
| 326 | .PP |
| 327 | .Vb 4 |
| 328 | \& switch(10,foo(7,3)) : expensive |
| 329 | \& Ugly('\e(") :Bad |
| 330 | \& _5x5 |
| 331 | \& locked method |
| 332 | .Ve |
| 333 | .PP |
| 334 | Some examples of syntactically invalid attribute lists (with annotation): |
| 335 | .PP |
| 336 | .Vb 5 |
| 337 | \& switch(10,foo() # ()-string not balanced |
| 338 | \& Ugly('(') # ()-string not balanced |
| 339 | \& 5x5 # "5x5" not a valid identifier |
| 340 | \& Y2::north # "Y2::north" not a simple identifier |
| 341 | \& foo + bar # "+" neither a colon nor whitespace |
| 342 | .Ve |
| 343 | .SH "EXPORTS" |
| 344 | .IX Header "EXPORTS" |
| 345 | .Sh "Default exports" |
| 346 | .IX Subsection "Default exports" |
| 347 | None. |
| 348 | .Sh "Available exports" |
| 349 | .IX Subsection "Available exports" |
| 350 | The routines \f(CW\*(C`get\*(C'\fR and \f(CW\*(C`reftype\*(C'\fR are exportable. |
| 351 | .Sh "Export tags defined" |
| 352 | .IX Subsection "Export tags defined" |
| 353 | The \f(CW\*(C`:ALL\*(C'\fR tag will get all of the above exports. |
| 354 | .SH "EXAMPLES" |
| 355 | .IX Header "EXAMPLES" |
| 356 | Here are some samples of syntactically valid declarations, with annotation |
| 357 | as to how they resolve internally into \f(CW\*(C`use attributes\*(C'\fR invocations by |
| 358 | perl. These examples are primarily useful to see how the \*(L"appropriate |
| 359 | package\*(R" is found for the possible method lookups for package-defined |
| 360 | attributes. |
| 361 | .IP "1." 4 |
| 362 | Code: |
| 363 | .Sp |
| 364 | .Vb 3 |
| 365 | \& package Canine; |
| 366 | \& package Dog; |
| 367 | \& my Canine $spot : Watchful ; |
| 368 | .Ve |
| 369 | .Sp |
| 370 | Effect: |
| 371 | .Sp |
| 372 | .Vb 2 |
| 373 | \& use attributes (); |
| 374 | \& attributes::->import(Canine => \e$spot, "Watchful"); |
| 375 | .Ve |
| 376 | .IP "2." 4 |
| 377 | Code: |
| 378 | .Sp |
| 379 | .Vb 2 |
| 380 | \& package Felis; |
| 381 | \& my $cat : Nervous; |
| 382 | .Ve |
| 383 | .Sp |
| 384 | Effect: |
| 385 | .Sp |
| 386 | .Vb 2 |
| 387 | \& use attributes (); |
| 388 | \& attributes::->import(Felis => \e$cat, "Nervous"); |
| 389 | .Ve |
| 390 | .IP "3." 4 |
| 391 | Code: |
| 392 | .Sp |
| 393 | .Vb 2 |
| 394 | \& package X; |
| 395 | \& sub foo : locked ; |
| 396 | .Ve |
| 397 | .Sp |
| 398 | Effect: |
| 399 | .Sp |
| 400 | .Vb 1 |
| 401 | \& use attributes X => \e&foo, "locked"; |
| 402 | .Ve |
| 403 | .IP "4." 4 |
| 404 | Code: |
| 405 | .Sp |
| 406 | .Vb 2 |
| 407 | \& package X; |
| 408 | \& sub Y::x : locked { 1 } |
| 409 | .Ve |
| 410 | .Sp |
| 411 | Effect: |
| 412 | .Sp |
| 413 | .Vb 1 |
| 414 | \& use attributes Y => \e&Y::x, "locked"; |
| 415 | .Ve |
| 416 | .IP "5." 4 |
| 417 | Code: |
| 418 | .Sp |
| 419 | .Vb 2 |
| 420 | \& package X; |
| 421 | \& sub foo { 1 } |
| 422 | .Ve |
| 423 | .Sp |
| 424 | .Vb 2 |
| 425 | \& package Y; |
| 426 | \& BEGIN { *bar = \e&X::foo; } |
| 427 | .Ve |
| 428 | .Sp |
| 429 | .Vb 2 |
| 430 | \& package Z; |
| 431 | \& sub Y::bar : locked ; |
| 432 | .Ve |
| 433 | .Sp |
| 434 | Effect: |
| 435 | .Sp |
| 436 | .Vb 1 |
| 437 | \& use attributes X => \e&X::foo, "locked"; |
| 438 | .Ve |
| 439 | .PP |
| 440 | This last example is purely for purposes of completeness. You should not |
| 441 | be trying to mess with the attributes of something in a package that's |
| 442 | not your own. |
| 443 | .SH "SEE ALSO" |
| 444 | .IX Header "SEE ALSO" |
| 445 | \&\*(L"Private Variables via \fImy()\fR\*(R" in perlsub and |
| 446 | \&\*(L"Subroutine Attributes\*(R" in perlsub for details on the basic declarations; |
| 447 | attrs for the obsolescent form of subroutine attribute specification |
| 448 | which this module replaces; |
| 449 | \&\*(L"use\*(R" in perlfunc for details on the normal invocation mechanism. |