Debian Codegraph @

upstream/mageia-cauldron/man3pm/Attribute::Handlers.3pm

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\"
56 .\" Required to disable full justification in groff 1.23.0.
57 .if n .ds AD l
58 .\" ========================================================================
59 .\"
60 .IX Title "Attribute::Handlers 3pm"
61 .TH Attribute::Handlers 3pm 2023-12-22 "perl v5.42.0" "Perl Programmers Reference Guide"
62 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
63 .\" way too many mistakes in technical documents.
64 .if n .ad l
65 .nh
66 .SH NAME
67 Attribute::Handlers \- Simpler definition of attribute handlers
68 .SH VERSION
69 .IX Header "VERSION"
70 This document describes version 1.03 of Attribute::Handlers.
71 .SH SYNOPSIS
72 .IX Header "SYNOPSIS"
73 .Vb 4
74 \& package MyClass;
75 \& require 5.006;
76 \& use Attribute::Handlers;
77 \& no warnings \*(Aqredefine\*(Aq;
78 \&
79 \&
80 \& sub Good : ATTR(SCALAR) {
81 \& my ($package, $symbol, $referent, $attr, $data) = @_;
82 \&
83 \& # Invoked for any scalar variable with a :Good attribute,
84 \& # provided the variable was declared in MyClass (or
85 \& # a derived class) or typed to MyClass.
86 \&
87 \& # Do whatever to $referent here (executed in CHECK phase).
88 \& ...
89 \& }
90 \&
91 \& sub Bad : ATTR(SCALAR) {
92 \& # Invoked for any scalar variable with a :Bad attribute,
93 \& # provided the variable was declared in MyClass (or
94 \& # a derived class) or typed to MyClass.
95 \& ...
96 \& }
97 \&
98 \& sub Good : ATTR(ARRAY) {
99 \& # Invoked for any array variable with a :Good attribute,
100 \& # provided the variable was declared in MyClass (or
101 \& # a derived class) or typed to MyClass.
102 \& ...
103 \& }
104 \&
105 \& sub Good : ATTR(HASH) {
106 \& # Invoked for any hash variable with a :Good attribute,
107 \& # provided the variable was declared in MyClass (or
108 \& # a derived class) or typed to MyClass.
109 \& ...
110 \& }
111 \&
112 \& sub Ugly : ATTR(CODE) {
113 \& # Invoked for any subroutine declared in MyClass (or a
114 \& # derived class) with an :Ugly attribute.
115 \& ...
116 \& }
117 \&
118 \& sub Omni : ATTR {
119 \& # Invoked for any scalar, array, hash, or subroutine
120 \& # with an :Omni attribute, provided the variable or
121 \& # subroutine was declared in MyClass (or a derived class)
122 \& # or the variable was typed to MyClass.
123 \& # Use ref($_[2]) to determine what kind of referent it was.
124 \& ...
125 \& }
126 \&
127 \&
128 \& use Attribute::Handlers autotie => { Cycle => Tie::Cycle };
129 \&
130 \& my $next : Cycle([\*(AqA\*(Aq..\*(AqZ\*(Aq]);
131 .Ve
132 .SH DESCRIPTION
133 .IX Header "DESCRIPTION"
134 This module, when inherited by a package, allows that package\*(Aqs class to
135 define attribute handler subroutines for specific attributes. Variables
136 and subroutines subsequently defined in that package, or in packages
137 derived from that package may be given attributes with the same names as
138 the attribute handler subroutines, which will then be called in one of
139 the compilation phases (i.e. in a \f(CW\*(C`BEGIN\*(C'\fR, \f(CW\*(C`CHECK\*(C'\fR, \f(CW\*(C`INIT\*(C'\fR, or \f(CW\*(C`END\*(C'\fR
140 block). (\f(CW\*(C`UNITCHECK\*(C'\fR blocks don\*(Aqt correspond to a global compilation
141 phase, so they can\*(Aqt be specified here.)
142 .PP
143 To create a handler, define it as a subroutine with the same name as
144 the desired attribute, and declare the subroutine itself with the
145 attribute \f(CW\*(C`:ATTR\*(C'\fR. For example:
146 .PP
147 .Vb 2
148 \& package LoudDecl;
149 \& use Attribute::Handlers;
150 \&
151 \& sub Loud :ATTR {
152 \& my ($package, $symbol, $referent, $attr, $data, $phase,
153 \& $filename, $linenum) = @_;
154 \& print STDERR
155 \& ref($referent), " ",
156 \& *{$symbol}{NAME}, " ",
157 \& "($referent) ", "was just declared ",
158 \& "and ascribed the ${attr} attribute ",
159 \& "with data ($data)\en",
160 \& "in phase $phase\en",
161 \& "in file $filename at line $linenum\en";
162 \& }
163 .Ve
164 .PP
165 This creates a handler for the attribute \f(CW\*(C`:Loud\*(C'\fR in the class LoudDecl.
166 Thereafter, any subroutine declared with a \f(CW\*(C`:Loud\*(C'\fR attribute in the class
167 LoudDecl:
168 .PP
169 .Vb 1
170 \& package LoudDecl;
171 \&
172 \& sub foo: Loud {...}
173 .Ve
174 .PP
175 causes the above handler to be invoked, and passed:
176 .IP [0] 4
177 .IX Item "[0]"
178 the name of the package into which it was declared;
179 .IP [1] 4
180 .IX Item "[1]"
181 a reference to the symbol table entry (typeglob) containing the subroutine;
182 .IP [2] 4
183 .IX Item "[2]"
184 a reference to the subroutine;
185 .IP [3] 4
186 .IX Item "[3]"
187 the name of the attribute;
188 .IP [4] 4
189 .IX Item "[4]"
190 any data associated with that attribute;
191 .IP [5] 4
192 .IX Item "[5]"
193 the name of the phase in which the handler is being invoked;
194 .IP [6] 4
195 .IX Item "[6]"
196 the filename in which the handler is being invoked;
197 .IP [7] 4
198 .IX Item "[7]"
199 the line number in this file.
200 .PP
201 Likewise, declaring any variables with the \f(CW\*(C`:Loud\*(C'\fR attribute within the
202 package:
203 .PP
204 .Vb 1
205 \& package LoudDecl;
206 \&
207 \& my $foo :Loud;
208 \& my @foo :Loud;
209 \& my %foo :Loud;
210 .Ve
211 .PP
212 will cause the handler to be called with a similar argument list (except,
213 of course, that \f(CW$_[2]\fR will be a reference to the variable).
214 .PP
215 The package name argument will typically be the name of the class into
216 which the subroutine was declared, but it may also be the name of a derived
217 class (since handlers are inherited).
218 .PP
219 If a lexical variable is given an attribute, there is no symbol table to
220 which it belongs, so the symbol table argument (\f(CW$_[1]\fR) is set to the
221 string \f(CW\*(AqLEXICAL\*(Aq\fR in that case. Likewise, ascribing an attribute to
222 an anonymous subroutine results in a symbol table argument of \f(CW\*(AqANON\*(Aq\fR.
223 .PP
224 The data argument passes in the value (if any) associated with the
225 attribute. For example, if \f(CW&foo\fR had been declared:
226 .PP
227 .Vb 1
228 \& sub foo :Loud("turn it up to 11, man!") {...}
229 .Ve
230 .PP
231 then a reference to an array containing the string
232 \&\f(CW"turn it up to 11, man!"\fR would be passed as the last argument.
233 .PP
234 Attribute::Handlers makes strenuous efforts to convert
235 the data argument (\f(CW$_[4]\fR) to a usable form before passing it to
236 the handler (but see "Non\-interpretive attribute handlers").
237 If those efforts succeed, the interpreted data is passed in an array
238 reference; if they fail, the raw data is passed as a string.
239 For example, all of these:
240 .PP
241 .Vb 4
242 \& sub foo :Loud(till=>ears=>are=>bleeding) {...}
243 \& sub foo :Loud(qw/till ears are bleeding/) {...}
244 \& sub foo :Loud(qw/till, ears, are, bleeding/) {...}
245 \& sub foo :Loud(till,ears,are,bleeding) {...}
246 .Ve
247 .PP
248 causes it to pass \f(CW\*(C`[\*(Aqtill\*(Aq,\*(Aqears\*(Aq,\*(Aqare\*(Aq,\*(Aqbleeding\*(Aq]\*(C'\fR as the handler\*(Aqs
249 data argument. While:
250 .PP
251 .Vb 1
252 \& sub foo :Loud([\*(Aqtill\*(Aq,\*(Aqears\*(Aq,\*(Aqare\*(Aq,\*(Aqbleeding\*(Aq]) {...}
253 .Ve
254 .PP
255 causes it to pass \f(CW\*(C`[ [\*(Aqtill\*(Aq,\*(Aqears\*(Aq,\*(Aqare\*(Aq,\*(Aqbleeding\*(Aq] ]\*(C'\fR; the array
256 reference specified in the data being passed inside the standard
257 array reference indicating successful interpretation.
258 .PP
259 However, if the data can\*(Aqt be parsed as valid Perl, then
260 it is passed as an uninterpreted string. For example:
261 .PP
262 .Vb 2
263 \& sub foo :Loud(my,ears,are,bleeding) {...}
264 \& sub foo :Loud(qw/my ears are bleeding) {...}
265 .Ve
266 .PP
267 cause the strings \f(CW\*(Aqmy,ears,are,bleeding\*(Aq\fR and
268 \&\f(CW\*(Aqqw/my ears are bleeding\*(Aq\fR respectively to be passed as the
269 data argument.
270 .PP
271 If no value is associated with the attribute, \f(CW\*(C`undef\*(C'\fR is passed.
272 .SS "Typed lexicals"
273 .IX Subsection "Typed lexicals"
274 Regardless of the package in which it is declared, if a lexical variable is
275 ascribed an attribute, the handler that is invoked is the one belonging to
276 the package to which it is typed. For example, the following declarations:
277 .PP
278 .Vb 1
279 \& package OtherClass;
280 \&
281 \& my LoudDecl $loudobj : Loud;
282 \& my LoudDecl @loudobjs : Loud;
283 \& my LoudDecl %loudobjex : Loud;
284 .Ve
285 .PP
286 causes the LoudDecl::Loud handler to be invoked (even if OtherClass also
287 defines a handler for \f(CW\*(C`:Loud\*(C'\fR attributes).
288 .SS "Type\-specific attribute handlers"
289 .IX Subsection "Type-specific attribute handlers"
290 If an attribute handler is declared and the \f(CW\*(C`:ATTR\*(C'\fR specifier is
291 given the name of a built\-in type (\f(CW\*(C`SCALAR\*(C'\fR, \f(CW\*(C`ARRAY\*(C'\fR, \f(CW\*(C`HASH\*(C'\fR, or \f(CW\*(C`CODE\*(C'\fR),
292 the handler is only applied to declarations of that type. For example,
293 the following definition:
294 .PP
295 .Vb 1
296 \& package LoudDecl;
297 \&
298 \& sub RealLoud :ATTR(SCALAR) { print "Yeeeeow!" }
299 .Ve
300 .PP
301 creates an attribute handler that applies only to scalars:
302 .PP
303 .Vb 2
304 \& package Painful;
305 \& use base LoudDecl;
306 \&
307 \& my $metal : RealLoud; # invokes &LoudDecl::RealLoud
308 \& my @metal : RealLoud; # error: unknown attribute
309 \& my %metal : RealLoud; # error: unknown attribute
310 \& sub metal : RealLoud {...} # error: unknown attribute
311 .Ve
312 .PP
313 You can, of course, declare separate handlers for these types as well
314 (but you\*(Aqll need to specify \f(CW\*(C`no warnings \*(Aqredefine\*(Aq\*(C'\fR to do it quietly):
315 .PP
316 .Vb 3
317 \& package LoudDecl;
318 \& use Attribute::Handlers;
319 \& no warnings \*(Aqredefine\*(Aq;
320 \&
321 \& sub RealLoud :ATTR(SCALAR) { print "Yeeeeow!" }
322 \& sub RealLoud :ATTR(ARRAY) { print "Urrrrrrrrrr!" }
323 \& sub RealLoud :ATTR(HASH) { print "Arrrrrgggghhhhhh!" }
324 \& sub RealLoud :ATTR(CODE) { croak "Real loud sub torpedoed" }
325 .Ve
326 .PP
327 You can also explicitly indicate that a single handler is meant to be
328 used for all types of referents like so:
329 .PP
330 .Vb 2
331 \& package LoudDecl;
332 \& use Attribute::Handlers;
333 \&
334 \& sub SeriousLoud :ATTR(ANY) { warn "Hearing loss imminent" }
335 .Ve
336 .PP
337 (I.e. \f(CWATTR(ANY)\fR is a synonym for \f(CW\*(C`:ATTR\*(C'\fR).
338 .SS "Non\-interpretive attribute handlers"
339 .IX Subsection "Non-interpretive attribute handlers"
340 Occasionally the strenuous efforts Attribute::Handlers makes to convert
341 the data argument (\f(CW$_[4]\fR) to a usable form before passing it to
342 the handler get in the way.
343 .PP
344 You can turn off that eagerness\-to\-help by declaring
345 an attribute handler with the keyword \f(CW\*(C`RAWDATA\*(C'\fR. For example:
346 .PP
347 .Vb 3
348 \& sub Raw : ATTR(RAWDATA) {...}
349 \& sub Nekkid : ATTR(SCALAR,RAWDATA) {...}
350 \& sub Au::Naturale : ATTR(RAWDATA,ANY) {...}
351 .Ve
352 .PP
353 Then the handler makes absolutely no attempt to interpret the data it
354 receives and simply passes it as a string:
355 .PP
356 .Vb 1
357 \& my $power : Raw(1..100); # handlers receives "1..100"
358 .Ve
359 .SS "Phase\-specific attribute handlers"
360 .IX Subsection "Phase-specific attribute handlers"
361 By default, attribute handlers are called at the end of the compilation
362 phase (in a \f(CW\*(C`CHECK\*(C'\fR block). This seems to be optimal in most cases because
363 most things that can be defined are defined by that point but nothing has
364 been executed.
365 .PP
366 However, it is possible to set up attribute handlers that are called at
367 other points in the program\*(Aqs compilation or execution, by explicitly
368 stating the phase (or phases) in which you wish the attribute handler to
369 be called. For example:
370 .PP
371 .Vb 5
372 \& sub Early :ATTR(SCALAR,BEGIN) {...}
373 \& sub Normal :ATTR(SCALAR,CHECK) {...}
374 \& sub Late :ATTR(SCALAR,INIT) {...}
375 \& sub Final :ATTR(SCALAR,END) {...}
376 \& sub Bookends :ATTR(SCALAR,BEGIN,END) {...}
377 .Ve
378 .PP
379 As the last example indicates, a handler may be set up to be (re)called in
380 two or more phases. The phase name is passed as the handler\*(Aqs final argument.
381 .PP
382 Note that attribute handlers that are scheduled for the \f(CW\*(C`BEGIN\*(C'\fR phase
383 are handled as soon as the attribute is detected (i.e. before any
384 subsequently defined \f(CW\*(C`BEGIN\*(C'\fR blocks are executed).
385 .ie n .SS "Attributes as ""tie"" interfaces"
386 .el .SS "Attributes as \f(CWtie\fP interfaces"
387 .IX Subsection "Attributes as tie interfaces"
388 Attributes make an excellent and intuitive interface through which to tie
389 variables. For example:
390 .PP
391 .Vb 2
392 \& use Attribute::Handlers;
393 \& use Tie::Cycle;
394 \&
395 \& sub UNIVERSAL::Cycle : ATTR(SCALAR) {
396 \& my ($package, $symbol, $referent, $attr, $data, $phase) = @_;
397 \& $data = [ $data ] unless ref $data eq \*(AqARRAY\*(Aq;
398 \& tie $$referent, \*(AqTie::Cycle\*(Aq, $data;
399 \& }
400 \&
401 \& # and thereafter...
402 \&
403 \& package main;
404 \&
405 \& my $next : Cycle(\*(AqA\*(Aq..\*(AqZ\*(Aq); # $next is now a tied variable
406 \&
407 \& while (<>) {
408 \& print $next;
409 \& }
410 .Ve
411 .PP
412 Note that, because the \f(CW\*(C`Cycle\*(C'\fR attribute receives its arguments in the
413 \&\f(CW$data\fR variable, if the attribute is given a list of arguments, \f(CW$data\fR
414 will consist of a single array reference; otherwise, it will consist of the
415 single argument directly. Since Tie::Cycle requires its cycling values to
416 be passed as an array reference, this means that we need to wrap
417 non\-array\-reference arguments in an array constructor:
418 .PP
419 .Vb 1
420 \& $data = [ $data ] unless ref $data eq \*(AqARRAY\*(Aq;
421 .Ve
422 .PP
423 Typically, however, things are the other way around: the tieable class expects
424 its arguments as a flattened list, so the attribute looks like:
425 .PP
426 .Vb 5
427 \& sub UNIVERSAL::Cycle : ATTR(SCALAR) {
428 \& my ($package, $symbol, $referent, $attr, $data, $phase) = @_;
429 \& my @data = ref $data eq \*(AqARRAY\*(Aq ? @$data : $data;
430 \& tie $$referent, \*(AqTie::Whatever\*(Aq, @data;
431 \& }
432 .Ve
433 .PP
434 This software pattern is so widely applicable that Attribute::Handlers
435 provides a way to automate it: specifying \f(CW\*(Aqautotie\*(Aq\fR in the
436 \&\f(CW\*(C`use Attribute::Handlers\*(C'\fR statement. So, the cycling example,
437 could also be written:
438 .PP
439 .Vb 1
440 \& use Attribute::Handlers autotie => { Cycle => \*(AqTie::Cycle\*(Aq };
441 \&
442 \& # and thereafter...
443 \&
444 \& package main;
445 \&
446 \& my $next : Cycle([\*(AqA\*(Aq..\*(AqZ\*(Aq]); # $next is now a tied variable
447 \&
448 \& while (<>) {
449 \& print $next;
450 \& }
451 .Ve
452 .PP
453 Note that we now have to pass the cycling values as an array reference,
454 since the \f(CW\*(C`autotie\*(C'\fR mechanism passes \f(CW\*(C`tie\*(C'\fR a list of arguments as a list
455 (as in the Tie::Whatever example), \fInot\fR as an array reference (as in
456 the original Tie::Cycle example at the start of this section).
457 .PP
458 The argument after \f(CW\*(Aqautotie\*(Aq\fR is a reference to a hash in which each key is
459 the name of an attribute to be created, and each value is the class to which
460 variables ascribed that attribute should be tied.
461 .PP
462 Note that there is no longer any need to import the Tie::Cycle module \-\-
463 Attribute::Handlers takes care of that automagically. You can even pass
464 arguments to the module\*(Aqs \f(CW\*(C`import\*(C'\fR subroutine, by appending them to the
465 class name. For example:
466 .PP
467 .Vb 2
468 \& use Attribute::Handlers
469 \& autotie => { Dir => \*(AqTie::Dir qw(DIR_UNLINK)\*(Aq };
470 .Ve
471 .PP
472 If the attribute name is unqualified, the attribute is installed in the
473 current package. Otherwise it is installed in the qualifier\*(Aqs package:
474 .PP
475 .Vb 1
476 \& package Here;
477 \&
478 \& use Attribute::Handlers autotie => {
479 \& Other::Good => Tie::SecureHash, # tie attr installed in Other::
480 \& Bad => Tie::Taxes, # tie attr installed in Here::
481 \& UNIVERSAL::Ugly => Software::Patent # tie attr installed everywhere
482 \& };
483 .Ve
484 .PP
485 Autoties are most commonly used in the module to which they actually tie,
486 and need to export their attributes to any module that calls them. To
487 facilitate this, Attribute::Handlers recognizes a special "pseudo\-class" \-\-
488 \&\f(CW\*(C`_\|_CALLER_\|_\*(C'\fR, which may be specified as the qualifier of an attribute:
489 .PP
490 .Vb 1
491 \& package Tie::Me::Kangaroo::Down::Sport;
492 \&
493 \& use Attribute::Handlers autotie =>
494 \& { \*(Aq_\|_CALLER_\|_::Roo\*(Aq => _\|_PACKAGE_\|_ };
495 .Ve
496 .PP
497 This causes Attribute::Handlers to define the \f(CW\*(C`Roo\*(C'\fR attribute in the package
498 that imports the Tie::Me::Kangaroo::Down::Sport module.
499 .PP
500 Note that it is important to quote the _\|_CALLER_\|_::Roo identifier because
501 a bug in perl 5.8 will refuse to parse it and cause an unknown error.
502 .PP
503 \fIPassing the tied object to \fR\f(CI\*(C`tie\*(C'\fR
504 .IX Subsection "Passing the tied object to tie"
505 .PP
506 Occasionally it is important to pass a reference to the object being tied
507 to the TIESCALAR, TIEHASH, etc. that ties it.
508 .PP
509 The \f(CW\*(C`autotie\*(C'\fR mechanism supports this too. The following code:
510 .PP
511 .Vb 2
512 \& use Attribute::Handlers autotieref => { Selfish => Tie::Selfish };
513 \& my $var : Selfish(@args);
514 .Ve
515 .PP
516 has the same effect as:
517 .PP
518 .Vb 1
519 \& tie my $var, \*(AqTie::Selfish\*(Aq, @args;
520 .Ve
521 .PP
522 But when \f(CW"autotieref"\fR is used instead of \f(CW"autotie"\fR:
523 .PP
524 .Vb 2
525 \& use Attribute::Handlers autotieref => { Selfish => Tie::Selfish };
526 \& my $var : Selfish(@args);
527 .Ve
528 .PP
529 the effect is to pass the \f(CW\*(C`tie\*(C'\fR call an extra reference to the variable
530 being tied:
531 .PP
532 .Vb 1
533 \& tie my $var, \*(AqTie::Selfish\*(Aq, \e$var, @args;
534 .Ve
535 .SH EXAMPLES
536 .IX Header "EXAMPLES"
537 If the class shown in "SYNOPSIS" were placed in the MyClass.pm
538 module, then the following code:
539 .PP
540 .Vb 2
541 \& package main;
542 \& use MyClass;
543 \&
544 \& my MyClass $slr :Good :Bad(1**1\-1) :Omni(\-vorous);
545 \&
546 \& package SomeOtherClass;
547 \& use base MyClass;
548 \&
549 \& sub tent { \*(Aqacle\*(Aq }
550 \&
551 \& sub fn :Ugly(sister) :Omni(\*(Aqpo\*(Aq,tent()) {...}
552 \& my @arr :Good :Omni(s/cie/nt/);
553 \& my %hsh :Good(q/bye/) :Omni(q/bus/);
554 .Ve
555 .PP
556 would cause the following handlers to be invoked:
557 .PP
558 .Vb 1
559 \& # my MyClass $slr :Good :Bad(1**1\-1) :Omni(\-vorous);
560 \&
561 \& MyClass::Good:ATTR(SCALAR)( \*(AqMyClass\*(Aq, # class
562 \& \*(AqLEXICAL\*(Aq, # no typeglob
563 \& \e$slr, # referent
564 \& \*(AqGood\*(Aq, # attr name
565 \& undef # no attr data
566 \& \*(AqCHECK\*(Aq, # compiler phase
567 \& );
568 \&
569 \& MyClass::Bad:ATTR(SCALAR)( \*(AqMyClass\*(Aq, # class
570 \& \*(AqLEXICAL\*(Aq, # no typeglob
571 \& \e$slr, # referent
572 \& \*(AqBad\*(Aq, # attr name
573 \& 0 # eval\*(Aqd attr data
574 \& \*(AqCHECK\*(Aq, # compiler phase
575 \& );
576 \&
577 \& MyClass::Omni:ATTR(SCALAR)( \*(AqMyClass\*(Aq, # class
578 \& \*(AqLEXICAL\*(Aq, # no typeglob
579 \& \e$slr, # referent
580 \& \*(AqOmni\*(Aq, # attr name
581 \& \*(Aq\-vorous\*(Aq # eval\*(Aqd attr data
582 \& \*(AqCHECK\*(Aq, # compiler phase
583 \& );
584 \&
585 \&
586 \& # sub fn :Ugly(sister) :Omni(\*(Aqpo\*(Aq,tent()) {...}
587 \&
588 \& MyClass::UGLY:ATTR(CODE)( \*(AqSomeOtherClass\*(Aq, # class
589 \& \e*SomeOtherClass::fn, # typeglob
590 \& \e&SomeOtherClass::fn, # referent
591 \& \*(AqUgly\*(Aq, # attr name
592 \& \*(Aqsister\*(Aq # eval\*(Aqd attr data
593 \& \*(AqCHECK\*(Aq, # compiler phase
594 \& );
595 \&
596 \& MyClass::Omni:ATTR(CODE)( \*(AqSomeOtherClass\*(Aq, # class
597 \& \e*SomeOtherClass::fn, # typeglob
598 \& \e&SomeOtherClass::fn, # referent
599 \& \*(AqOmni\*(Aq, # attr name
600 \& [\*(Aqpo\*(Aq,\*(Aqacle\*(Aq] # eval\*(Aqd attr data
601 \& \*(AqCHECK\*(Aq, # compiler phase
602 \& );
603 \&
604 \&
605 \& # my @arr :Good :Omni(s/cie/nt/);
606 \&
607 \& MyClass::Good:ATTR(ARRAY)( \*(AqSomeOtherClass\*(Aq, # class
608 \& \*(AqLEXICAL\*(Aq, # no typeglob
609 \& \e@arr, # referent
610 \& \*(AqGood\*(Aq, # attr name
611 \& undef # no attr data
612 \& \*(AqCHECK\*(Aq, # compiler phase
613 \& );
614 \&
615 \& MyClass::Omni:ATTR(ARRAY)( \*(AqSomeOtherClass\*(Aq, # class
616 \& \*(AqLEXICAL\*(Aq, # no typeglob
617 \& \e@arr, # referent
618 \& \*(AqOmni\*(Aq, # attr name
619 \& "" # eval\*(Aqd attr data
620 \& \*(AqCHECK\*(Aq, # compiler phase
621 \& );
622 \&
623 \&
624 \& # my %hsh :Good(q/bye) :Omni(q/bus/);
625 \&
626 \& MyClass::Good:ATTR(HASH)( \*(AqSomeOtherClass\*(Aq, # class
627 \& \*(AqLEXICAL\*(Aq, # no typeglob
628 \& \e%hsh, # referent
629 \& \*(AqGood\*(Aq, # attr name
630 \& \*(Aqq/bye\*(Aq # raw attr data
631 \& \*(AqCHECK\*(Aq, # compiler phase
632 \& );
633 \&
634 \& MyClass::Omni:ATTR(HASH)( \*(AqSomeOtherClass\*(Aq, # class
635 \& \*(AqLEXICAL\*(Aq, # no typeglob
636 \& \e%hsh, # referent
637 \& \*(AqOmni\*(Aq, # attr name
638 \& \*(Aqbus\*(Aq # eval\*(Aqd attr data
639 \& \*(AqCHECK\*(Aq, # compiler phase
640 \& );
641 .Ve
642 .PP
643 Installing handlers into UNIVERSAL, makes them...err..universal.
644 For example:
645 .PP
646 .Vb 2
647 \& package Descriptions;
648 \& use Attribute::Handlers;
649 \&
650 \& my %name;
651 \& sub name { return $name{$_[2]}||*{$_[1]}{NAME} }
652 \&
653 \& sub UNIVERSAL::Name :ATTR {
654 \& $name{$_[2]} = $_[4];
655 \& }
656 \&
657 \& sub UNIVERSAL::Purpose :ATTR {
658 \& print STDERR "Purpose of ", &name, " is $_[4]\en";
659 \& }
660 \&
661 \& sub UNIVERSAL::Unit :ATTR {
662 \& print STDERR &name, " measured in $_[4]\en";
663 \& }
664 .Ve
665 .PP
666 Let\*(Aqs you write:
667 .PP
668 .Vb 1
669 \& use Descriptions;
670 \&
671 \& my $capacity : Name(capacity)
672 \& : Purpose(to store max storage capacity for files)
673 \& : Unit(Gb);
674 \&
675 \&
676 \& package Other;
677 \&
678 \& sub foo : Purpose(to foo all data before barring it) { }
679 \&
680 \& # etc.
681 .Ve
682 .SH "UTILITY FUNCTIONS"
683 .IX Header "UTILITY FUNCTIONS"
684 This module offers a single utility function, \f(CWfindsym()\fR.
685 .IP findsym 4
686 .IX Item "findsym"
687 .Vb 1
688 \& my $symbol = Attribute::Handlers::findsym($package, $referent);
689 .Ve
690 .Sp
691 The function looks in the symbol table of \f(CW$package\fR for the typeglob for
692 \&\f(CW$referent\fR, which is a reference to a variable or subroutine (SCALAR, ARRAY,
693 HASH, or CODE). If it finds the typeglob, it returns it. Otherwise, it returns
694 undef. Note that \f(CW\*(C`findsym\*(C'\fR memoizes the typeglobs it has previously
695 successfully found, so subsequent calls with the same arguments should be
696 much faster.
697 .SH DIAGNOSTICS
698 .IX Header "DIAGNOSTICS"
699 .ie n .IP """Bad attribute type: ATTR(%s)""" 4
700 .el .IP "\f(CWBad attribute type: ATTR(%s)\fR" 4
701 .IX Item "Bad attribute type: ATTR(%s)"
702 An attribute handler was specified with an \f(CW:ATTR(\fR\f(CIref_type\fR\f(CW)\fR, but the
703 type of referent it was defined to handle wasn\*(Aqt one of the five permitted:
704 \&\f(CW\*(C`SCALAR\*(C'\fR, \f(CW\*(C`ARRAY\*(C'\fR, \f(CW\*(C`HASH\*(C'\fR, \f(CW\*(C`CODE\*(C'\fR, or \f(CW\*(C`ANY\*(C'\fR.
705 .ie n .IP """Attribute handler %s doesn\*(Aqt handle %s attributes""" 4
706 .el .IP "\f(CWAttribute handler %s doesn\*(Aqt handle %s attributes\fR" 4
707 .IX Item "Attribute handler %s doesn't handle %s attributes"
708 A handler for attributes of the specified name \fIwas\fR defined, but not
709 for the specified type of declaration. Typically encountered when trying
710 to apply a \f(CW\*(C`VAR\*(C'\fR attribute handler to a subroutine, or a \f(CW\*(C`SCALAR\*(C'\fR
711 attribute handler to some other type of variable.
712 .ie n .IP """Declaration of %s attribute in package %s may clash with future reserved word""" 4
713 .el .IP "\f(CWDeclaration of %s attribute in package %s may clash with future reserved word\fR" 4
714 .IX Item "Declaration of %s attribute in package %s may clash with future reserved word"
715 A handler for an attributes with an all\-lowercase name was declared. An
716 attribute with an all\-lowercase name might have a meaning to Perl
717 itself some day, even though most don\*(Aqt yet. Use a mixed\-case attribute
718 name, instead.
719 .ie n .IP """Can\*(Aqt have two ATTR specifiers on one subroutine""" 4
720 .el .IP "\f(CWCan\*(Aqt have two ATTR specifiers on one subroutine\fR" 4
721 .IX Item "Can't have two ATTR specifiers on one subroutine"
722 You just can\*(Aqt, okay?
723 Instead, put all the specifications together with commas between them
724 in a single \f(CWATTR(\fR\f(CIspecification\fR\f(CW)\fR.
725 .ie n .IP """Can\*(Aqt autotie a %s""" 4
726 .el .IP "\f(CWCan\*(Aqt autotie a %s\fR" 4
727 .IX Item "Can't autotie a %s"
728 You can only declare autoties for types \f(CW"SCALAR"\fR, \f(CW"ARRAY"\fR, and
729 \&\f(CW"HASH"\fR. They\*(Aqre the only things (apart from typeglobs \-\- which are
730 not declarable) that Perl can tie.
731 .ie n .IP """Internal error: %s symbol went missing""" 4
732 .el .IP "\f(CWInternal error: %s symbol went missing\fR" 4
733 .IX Item "Internal error: %s symbol went missing"
734 Something is rotten in the state of the program. An attributed
735 subroutine ceased to exist between the point it was declared and the point
736 at which its attribute handler(s) would have been called.
737 .ie n .IP """Won\*(Aqt be able to apply END handler""" 4
738 .el .IP "\f(CWWon\*(Aqt be able to apply END handler\fR" 4
739 .IX Item "Won't be able to apply END handler"
740 You have defined an END handler for an attribute that is being applied
741 to a lexical variable. Since the variable may not be available during END
742 this won\*(Aqt happen.
743 .SH AUTHOR
744 .IX Header "AUTHOR"
745 Damian Conway ([email protected]). The maintainer of this module is now Rafael
746 Garcia\-Suarez ([email protected]).
747 .PP
748 Maintainer of the CPAN release is Steffen Mueller ([email protected]).
749 Contact him with technical difficulties with respect to the packaging of the
750 CPAN module.
751 .SH BUGS
752 .IX Header "BUGS"
753 There are undoubtedly serious bugs lurking somewhere in code this funky :\-)
754 Bug reports and other feedback are most welcome.
755 .SH "COPYRIGHT AND LICENSE"
756 .IX Header "COPYRIGHT AND LICENSE"
757 .Vb 3
758 \& Copyright (c) 2001\-2014, Damian Conway. All Rights Reserved.
759 \& This module is free software. It may be used, redistributed
760 \& and/or modified under the same terms as Perl itself.
761 .Ve