Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / perl-5.8.0 / man / man3 / Attribute::Handlers.3
CommitLineData
86530b38
AT
1.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13
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 "Attribute::Handlers 3"
132.TH Attribute::Handlers 3 "2002-06-01" "perl v5.8.0" "Perl Programmers Reference Guide"
133.SH "NAME"
134Attribute::Handlers \- Simpler definition of attribute handlers
135.SH "VERSION"
136.IX Header "VERSION"
137This document describes version 0.77 of Attribute::Handlers,
138released June 8, 2002.
139.SH "SYNOPSIS"
140.IX Header "SYNOPSIS"
141.Vb 4
142\& package MyClass;
143\& require v5.6.0;
144\& use Attribute::Handlers;
145\& no warnings 'redefine';
146.Ve
147.PP
148.Vb 2
149\& sub Good : ATTR(SCALAR) {
150\& my ($package, $symbol, $referent, $attr, $data) = @_;
151.Ve
152.PP
153.Vb 3
154\& # Invoked for any scalar variable with a :Good attribute,
155\& # provided the variable was declared in MyClass (or
156\& # a derived class) or typed to MyClass.
157.Ve
158.PP
159.Vb 3
160\& # Do whatever to $referent here (executed in CHECK phase).
161\& ...
162\& }
163.Ve
164.PP
165.Vb 6
166\& sub Bad : ATTR(SCALAR) {
167\& # Invoked for any scalar variable with a :Bad attribute,
168\& # provided the variable was declared in MyClass (or
169\& # a derived class) or typed to MyClass.
170\& ...
171\& }
172.Ve
173.PP
174.Vb 6
175\& sub Good : ATTR(ARRAY) {
176\& # Invoked for any array variable with a :Good attribute,
177\& # provided the variable was declared in MyClass (or
178\& # a derived class) or typed to MyClass.
179\& ...
180\& }
181.Ve
182.PP
183.Vb 6
184\& sub Good : ATTR(HASH) {
185\& # Invoked for any hash variable with a :Good attribute,
186\& # provided the variable was declared in MyClass (or
187\& # a derived class) or typed to MyClass.
188\& ...
189\& }
190.Ve
191.PP
192.Vb 5
193\& sub Ugly : ATTR(CODE) {
194\& # Invoked for any subroutine declared in MyClass (or a
195\& # derived class) with an :Ugly attribute.
196\& ...
197\& }
198.Ve
199.PP
200.Vb 8
201\& sub Omni : ATTR {
202\& # Invoked for any scalar, array, hash, or subroutine
203\& # with an :Omni attribute, provided the variable or
204\& # subroutine was declared in MyClass (or a derived class)
205\& # or the variable was typed to MyClass.
206\& # Use ref($_[2]) to determine what kind of referent it was.
207\& ...
208\& }
209.Ve
210.PP
211.Vb 1
212\& use Attribute::Handlers autotie => { Cycle => Tie::Cycle };
213.Ve
214.PP
215.Vb 1
216\& my $next : Cycle(['A'..'Z']);
217.Ve
218.SH "DESCRIPTION"
219.IX Header "DESCRIPTION"
220This module, when inherited by a package, allows that package's class to
221define attribute handler subroutines for specific attributes. Variables
222and subroutines subsequently defined in that package, or in packages
223derived from that package may be given attributes with the same names as
224the attribute handler subroutines, which will then be called in one of
225the 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
226block).
227.PP
228To create a handler, define it as a subroutine with the same name as
229the desired attribute, and declare the subroutine itself with the
230attribute \f(CW\*(C`:ATTR\*(C'\fR. For example:
231.PP
232.Vb 2
233\& package LoudDecl;
234\& use Attribute::Handlers;
235.Ve
236.PP
237.Vb 10
238\& sub Loud :ATTR {
239\& my ($package, $symbol, $referent, $attr, $data, $phase) = @_;
240\& print STDERR
241\& ref($referent), " ",
242\& *{$symbol}{NAME}, " ",
243\& "($referent) ", "was just declared ",
244\& "and ascribed the ${attr} attribute ",
245\& "with data ($data)\en",
246\& "in phase $phase\en";
247\& }
248.Ve
249.PP
250This creates a handler for the attribute \f(CW\*(C`:Loud\*(C'\fR in the class LoudDecl.
251Thereafter, any subroutine declared with a \f(CW\*(C`:Loud\*(C'\fR attribute in the class
252LoudDecl:
253.PP
254.Vb 1
255\& package LoudDecl;
256.Ve
257.PP
258.Vb 1
259\& sub foo: Loud {...}
260.Ve
261.PP
262causes the above handler to be invoked, and passed:
263.IP "[0]" 4
264.IX Item "[0]"
265the name of the package into which it was declared;
266.IP "[1]" 4
267.IX Item "[1]"
268a reference to the symbol table entry (typeglob) containing the subroutine;
269.IP "[2]" 4
270.IX Item "[2]"
271a reference to the subroutine;
272.IP "[3]" 4
273.IX Item "[3]"
274the name of the attribute;
275.IP "[4]" 4
276.IX Item "[4]"
277any data associated with that attribute;
278.IP "[5]" 4
279.IX Item "[5]"
280the name of the phase in which the handler is being invoked.
281.PP
282Likewise, declaring any variables with the \f(CW\*(C`:Loud\*(C'\fR attribute within the
283package:
284.PP
285.Vb 1
286\& package LoudDecl;
287.Ve
288.PP
289.Vb 3
290\& my $foo :Loud;
291\& my @foo :Loud;
292\& my %foo :Loud;
293.Ve
294.PP
295will cause the handler to be called with a similar argument list (except,
296of course, that \f(CW$_[2]\fR will be a reference to the variable).
297.PP
298The package name argument will typically be the name of the class into
299which the subroutine was declared, but it may also be the name of a derived
300class (since handlers are inherited).
301.PP
302If a lexical variable is given an attribute, there is no symbol table to
303which it belongs, so the symbol table argument (\f(CW$_[1]\fR) is set to the
304string \f(CW'LEXICAL'\fR in that case. Likewise, ascribing an attribute to
305an anonymous subroutine results in a symbol table argument of \f(CW'ANON'\fR.
306.PP
307The data argument passes in the value (if any) associated with the
308attribute. For example, if \f(CW&foo\fR had been declared:
309.PP
310.Vb 1
311\& sub foo :Loud("turn it up to 11, man!") {...}
312.Ve
313.PP
314then the string \f(CW"turn it up to 11, man!"\fR would be passed as the
315last argument.
316.PP
317Attribute::Handlers makes strenuous efforts to convert
318the data argument (\f(CW$_[4]\fR) to a useable form before passing it to
319the handler (but see \*(L"Non\-interpretive attribute handlers\*(R").
320For example, all of these:
321.PP
322.Vb 5
323\& sub foo :Loud(till=>ears=>are=>bleeding) {...}
324\& sub foo :Loud(['till','ears','are','bleeding']) {...}
325\& sub foo :Loud(qw/till ears are bleeding/) {...}
326\& sub foo :Loud(qw/my, ears, are, bleeding/) {...}
327\& sub foo :Loud(till,ears,are,bleeding) {...}
328.Ve
329.PP
330causes it to pass \f(CW\*(C`['till','ears','are','bleeding']\*(C'\fR as the handler's
331data argument. However, if the data can't be parsed as valid Perl, then
332it is passed as an uninterpreted string. For example:
333.PP
334.Vb 2
335\& sub foo :Loud(my,ears,are,bleeding) {...}
336\& sub foo :Loud(qw/my ears are bleeding) {...}
337.Ve
338.PP
339cause the strings \f(CW'my,ears,are,bleeding'\fR and \f(CW'qw/my ears are bleeding'\fR
340respectively to be passed as the data argument.
341.PP
342If the attribute has only a single associated scalar data value, that value is
343passed as a scalar. If multiple values are associated, they are passed as an
344array reference. If no value is associated with the attribute, \f(CW\*(C`undef\*(C'\fR is
345passed.
346.Sh "Typed lexicals"
347.IX Subsection "Typed lexicals"
348Regardless of the package in which it is declared, if a lexical variable is
349ascribed an attribute, the handler that is invoked is the one belonging to
350the package to which it is typed. For example, the following declarations:
351.PP
352.Vb 1
353\& package OtherClass;
354.Ve
355.PP
356.Vb 3
357\& my LoudDecl $loudobj : Loud;
358\& my LoudDecl @loudobjs : Loud;
359\& my LoudDecl %loudobjex : Loud;
360.Ve
361.PP
362causes the LoudDecl::Loud handler to be invoked (even if OtherClass also
363defines a handler for \f(CW\*(C`:Loud\*(C'\fR attributes).
364.Sh "Type-specific attribute handlers"
365.IX Subsection "Type-specific attribute handlers"
366If an attribute handler is declared and the \f(CW\*(C`:ATTR\*(C'\fR specifier is
367given 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),
368the handler is only applied to declarations of that type. For example,
369the following definition:
370.PP
371.Vb 1
372\& package LoudDecl;
373.Ve
374.PP
375.Vb 1
376\& sub RealLoud :ATTR(SCALAR) { print "Yeeeeow!" }
377.Ve
378.PP
379creates an attribute handler that applies only to scalars:
380.PP
381.Vb 2
382\& package Painful;
383\& use base LoudDecl;
384.Ve
385.PP
386.Vb 4
387\& my $metal : RealLoud; # invokes &LoudDecl::RealLoud
388\& my @metal : RealLoud; # error: unknown attribute
389\& my %metal : RealLoud; # error: unknown attribute
390\& sub metal : RealLoud {...} # error: unknown attribute
391.Ve
392.PP
393You can, of course, declare separate handlers for these types as well
394(but you'll need to specify \f(CW\*(C`no warnings 'redefine'\*(C'\fR to do it quietly):
395.PP
396.Vb 3
397\& package LoudDecl;
398\& use Attribute::Handlers;
399\& no warnings 'redefine';
400.Ve
401.PP
402.Vb 4
403\& sub RealLoud :ATTR(SCALAR) { print "Yeeeeow!" }
404\& sub RealLoud :ATTR(ARRAY) { print "Urrrrrrrrrr!" }
405\& sub RealLoud :ATTR(HASH) { print "Arrrrrgggghhhhhh!" }
406\& sub RealLoud :ATTR(CODE) { croak "Real loud sub torpedoed" }
407.Ve
408.PP
409You can also explicitly indicate that a single handler is meant to be
410used for all types of referents like so:
411.PP
412.Vb 2
413\& package LoudDecl;
414\& use Attribute::Handlers;
415.Ve
416.PP
417.Vb 1
418\& sub SeriousLoud :ATTR(ANY) { warn "Hearing loss imminent" }
419.Ve
420.PP
421(I.e. \f(CW\*(C`ATTR(ANY)\*(C'\fR is a synonym for \f(CW\*(C`:ATTR\*(C'\fR).
422.Sh "Non-interpretive attribute handlers"
423.IX Subsection "Non-interpretive attribute handlers"
424Occasionally the strenuous efforts Attribute::Handlers makes to convert
425the data argument (\f(CW$_[4]\fR) to a useable form before passing it to
426the handler get in the way.
427.PP
428You can turn off that eagerness-to-help by declaring
429an attribute handler with the keyword \f(CW\*(C`RAWDATA\*(C'\fR. For example:
430.PP
431.Vb 3
432\& sub Raw : ATTR(RAWDATA) {...}
433\& sub Nekkid : ATTR(SCALAR,RAWDATA) {...}
434\& sub Au::Naturale : ATTR(RAWDATA,ANY) {...}
435.Ve
436.PP
437Then the handler makes absolutely no attempt to interpret the data it
438receives and simply passes it as a string:
439.PP
440.Vb 1
441\& my $power : Raw(1..100); # handlers receives "1..100"
442.Ve
443.Sh "Phase-specific attribute handlers"
444.IX Subsection "Phase-specific attribute handlers"
445By default, attribute handlers are called at the end of the compilation
446phase (in a \f(CW\*(C`CHECK\*(C'\fR block). This seems to be optimal in most cases because
447most things that can be defined are defined by that point but nothing has
448been executed.
449.PP
450However, it is possible to set up attribute handlers that are called at
451other points in the program's compilation or execution, by explicitly
452stating the phase (or phases) in which you wish the attribute handler to
453be called. For example:
454.PP
455.Vb 5
456\& sub Early :ATTR(SCALAR,BEGIN) {...}
457\& sub Normal :ATTR(SCALAR,CHECK) {...}
458\& sub Late :ATTR(SCALAR,INIT) {...}
459\& sub Final :ATTR(SCALAR,END) {...}
460\& sub Bookends :ATTR(SCALAR,BEGIN,END) {...}
461.Ve
462.PP
463As the last example indicates, a handler may be set up to be (re)called in
464two or more phases. The phase name is passed as the handler's final argument.
465.PP
466Note that attribute handlers that are scheduled for the \f(CW\*(C`BEGIN\*(C'\fR phase
467are handled as soon as the attribute is detected (i.e. before any
468subsequently defined \f(CW\*(C`BEGIN\*(C'\fR blocks are executed).
469.ie n .Sh "Attributes as ""tie"" interfaces"
470.el .Sh "Attributes as \f(CWtie\fP interfaces"
471.IX Subsection "Attributes as tie interfaces"
472Attributes make an excellent and intuitive interface through which to tie
473variables. For example:
474.PP
475.Vb 2
476\& use Attribute::Handlers;
477\& use Tie::Cycle;
478.Ve
479.PP
480.Vb 5
481\& sub UNIVERSAL::Cycle : ATTR(SCALAR) {
482\& my ($package, $symbol, $referent, $attr, $data, $phase) = @_;
483\& $data = [ $data ] unless ref $data eq 'ARRAY';
484\& tie $$referent, 'Tie::Cycle', $data;
485\& }
486.Ve
487.PP
488.Vb 1
489\& # and thereafter...
490.Ve
491.PP
492.Vb 1
493\& package main;
494.Ve
495.PP
496.Vb 1
497\& my $next : Cycle('A'..'Z'); # $next is now a tied variable
498.Ve
499.PP
500.Vb 3
501\& while (<>) {
502\& print $next;
503\& }
504.Ve
505.PP
506Note that, because the \f(CW\*(C`Cycle\*(C'\fR attribute receives its arguments in the
507\&\f(CW$data\fR variable, if the attribute is given a list of arguments, \f(CW$data\fR
508will consist of a single array reference; otherwise, it will consist of the
509single argument directly. Since Tie::Cycle requires its cycling values to
510be passed as an array reference, this means that we need to wrap
511non-array-reference arguments in an array constructor:
512.PP
513.Vb 1
514\& $data = [ $data ] unless ref $data eq 'ARRAY';
515.Ve
516.PP
517Typically, however, things are the other way around: the tieable class expects
518its arguments as a flattened list, so the attribute looks like:
519.PP
520.Vb 5
521\& sub UNIVERSAL::Cycle : ATTR(SCALAR) {
522\& my ($package, $symbol, $referent, $attr, $data, $phase) = @_;
523\& my @data = ref $data eq 'ARRAY' ? @$data : $data;
524\& tie $$referent, 'Tie::Whatever', @data;
525\& }
526.Ve
527.PP
528This software pattern is so widely applicable that Attribute::Handlers
529provides a way to automate it: specifying \f(CW'autotie'\fR in the
530\&\f(CW\*(C`use Attribute::Handlers\*(C'\fR statement. So, the cycling example,
531could also be written:
532.PP
533.Vb 1
534\& use Attribute::Handlers autotie => { Cycle => 'Tie::Cycle' };
535.Ve
536.PP
537.Vb 1
538\& # and thereafter...
539.Ve
540.PP
541.Vb 1
542\& package main;
543.Ve
544.PP
545.Vb 1
546\& my $next : Cycle(['A'..'Z']); # $next is now a tied variable
547.Ve
548.PP
549.Vb 2
550\& while (<>) {
551\& print $next;
552.Ve
553.PP
554Note that we now have to pass the cycling values as an array reference,
555since the \f(CW\*(C`autotie\*(C'\fR mechanism passes \f(CW\*(C`tie\*(C'\fR a list of arguments as a list
556(as in the Tie::Whatever example), \fInot\fR as an array reference (as in
557the original Tie::Cycle example at the start of this section).
558.PP
559The argument after \f(CW'autotie'\fR is a reference to a hash in which each key is
560the name of an attribute to be created, and each value is the class to which
561variables ascribed that attribute should be tied.
562.PP
563Note that there is no longer any need to import the Tie::Cycle module \*(--
564Attribute::Handlers takes care of that automagically. You can even pass
565arguments to the module's \f(CW\*(C`import\*(C'\fR subroutine, by appending them to the
566class name. For example:
567.PP
568.Vb 2
569\& use Attribute::Handlers
570\& autotie => { Dir => 'Tie::Dir qw(DIR_UNLINK)' };
571.Ve
572.PP
573If the attribute name is unqualified, the attribute is installed in the
574current package. Otherwise it is installed in the qualifier's package:
575.PP
576.Vb 1
577\& package Here;
578.Ve
579.PP
580.Vb 5
581\& use Attribute::Handlers autotie => {
582\& Other::Good => Tie::SecureHash, # tie attr installed in Other::
583\& Bad => Tie::Taxes, # tie attr installed in Here::
584\& UNIVERSAL::Ugly => Software::Patent # tie attr installed everywhere
585\& };
586.Ve
587.PP
588Autoties are most commonly used in the module to which they actually tie,
589and need to export their attributes to any module that calls them. To
590facilitiate this, Attribute::Handlers recognizes a special \*(L"pseudo\-class\*(R" \*(--
591\&\f(CW\*(C`_\|_CALLER_\|_\*(C'\fR, which may be specified as the qualifier of an attribute:
592.PP
593.Vb 1
594\& package Tie::Me::Kangaroo:Down::Sport;
595.Ve
596.PP
597.Vb 1
598\& use Attribute::Handlers autotie => { __CALLER__::Roo => __PACKAGE__ };
599.Ve
600.PP
601This causes Attribute::Handlers to define the \f(CW\*(C`Roo\*(C'\fR attribute in the package
602that imports the Tie::Me::Kangaroo:Down::Sport module.
603.PP
604\fIPassing the tied object to \f(CI\*(C`tie\*(C'\fI\fR
605.IX Subsection "Passing the tied object to tie"
606.PP
607Occasionally it is important to pass a reference to the object being tied
608to the \s-1TIESCALAR\s0, \s-1TIEHASH\s0, etc. that ties it.
609.PP
610The \f(CW\*(C`autotie\*(C'\fR mechanism supports this too. The following code:
611.PP
612.Vb 2
613\& use Attribute::Handlers autotieref => { Selfish => Tie::Selfish };
614\& my $var : Selfish(@args);
615.Ve
616.PP
617has the same effect as:
618.PP
619.Vb 1
620\& tie my $var, 'Tie::Selfish', @args;
621.Ve
622.PP
623But when \f(CW"autotieref"\fR is used instead of \f(CW"autotie"\fR:
624.PP
625.Vb 2
626\& use Attribute::Handlers autotieref => { Selfish => Tie::Selfish };
627\& my $var : Selfish(@args);
628.Ve
629.PP
630the effect is to pass the \f(CW\*(C`tie\*(C'\fR call an extra reference to the variable
631being tied:
632.PP
633.Vb 1
634\& tie my $var, 'Tie::Selfish', \e$var, @args;
635.Ve
636.SH "EXAMPLES"
637.IX Header "EXAMPLES"
638If the class shown in \s-1SYNOPSIS\s0 were placed in the MyClass.pm
639module, then the following code:
640.PP
641.Vb 2
642\& package main;
643\& use MyClass;
644.Ve
645.PP
646.Vb 1
647\& my MyClass $slr :Good :Bad(1**1-1) :Omni(-vorous);
648.Ve
649.PP
650.Vb 2
651\& package SomeOtherClass;
652\& use base MyClass;
653.Ve
654.PP
655.Vb 1
656\& sub tent { 'acle' }
657.Ve
658.PP
659.Vb 3
660\& sub fn :Ugly(sister) :Omni('po',tent()) {...}
661\& my @arr :Good :Omni(s/cie/nt/);
662\& my %hsh :Good(q/bye) :Omni(q/bus/);
663.Ve
664.PP
665would cause the following handlers to be invoked:
666.PP
667.Vb 1
668\& # my MyClass $slr :Good :Bad(1**1-1) :Omni(-vorous);
669.Ve
670.PP
671.Vb 7
672\& MyClass::Good:ATTR(SCALAR)( 'MyClass', # class
673\& 'LEXICAL', # no typeglob
674\& \e$slr, # referent
675\& 'Good', # attr name
676\& undef # no attr data
677\& 'CHECK', # compiler phase
678\& );
679.Ve
680.PP
681.Vb 7
682\& MyClass::Bad:ATTR(SCALAR)( 'MyClass', # class
683\& 'LEXICAL', # no typeglob
684\& \e$slr, # referent
685\& 'Bad', # attr name
686\& 0 # eval'd attr data
687\& 'CHECK', # compiler phase
688\& );
689.Ve
690.PP
691.Vb 7
692\& MyClass::Omni:ATTR(SCALAR)( 'MyClass', # class
693\& 'LEXICAL', # no typeglob
694\& \e$slr, # referent
695\& 'Omni', # attr name
696\& '-vorous' # eval'd attr data
697\& 'CHECK', # compiler phase
698\& );
699.Ve
700.PP
701.Vb 1
702\& # sub fn :Ugly(sister) :Omni('po',tent()) {...}
703.Ve
704.PP
705.Vb 7
706\& MyClass::UGLY:ATTR(CODE)( 'SomeOtherClass', # class
707\& \e*SomeOtherClass::fn, # typeglob
708\& \e&SomeOtherClass::fn, # referent
709\& 'Ugly', # attr name
710\& 'sister' # eval'd attr data
711\& 'CHECK', # compiler phase
712\& );
713.Ve
714.PP
715.Vb 7
716\& MyClass::Omni:ATTR(CODE)( 'SomeOtherClass', # class
717\& \e*SomeOtherClass::fn, # typeglob
718\& \e&SomeOtherClass::fn, # referent
719\& 'Omni', # attr name
720\& ['po','acle'] # eval'd attr data
721\& 'CHECK', # compiler phase
722\& );
723.Ve
724.PP
725.Vb 1
726\& # my @arr :Good :Omni(s/cie/nt/);
727.Ve
728.PP
729.Vb 7
730\& MyClass::Good:ATTR(ARRAY)( 'SomeOtherClass', # class
731\& 'LEXICAL', # no typeglob
732\& \e@arr, # referent
733\& 'Good', # attr name
734\& undef # no attr data
735\& 'CHECK', # compiler phase
736\& );
737.Ve
738.PP
739.Vb 7
740\& MyClass::Omni:ATTR(ARRAY)( 'SomeOtherClass', # class
741\& 'LEXICAL', # no typeglob
742\& \e@arr, # referent
743\& 'Omni', # attr name
744\& "" # eval'd attr data
745\& 'CHECK', # compiler phase
746\& );
747.Ve
748.PP
749.Vb 1
750\& # my %hsh :Good(q/bye) :Omni(q/bus/);
751.Ve
752.PP
753.Vb 7
754\& MyClass::Good:ATTR(HASH)( 'SomeOtherClass', # class
755\& 'LEXICAL', # no typeglob
756\& \e%hsh, # referent
757\& 'Good', # attr name
758\& 'q/bye' # raw attr data
759\& 'CHECK', # compiler phase
760\& );
761.Ve
762.PP
763.Vb 7
764\& MyClass::Omni:ATTR(HASH)( 'SomeOtherClass', # class
765\& 'LEXICAL', # no typeglob
766\& \e%hsh, # referent
767\& 'Omni', # attr name
768\& 'bus' # eval'd attr data
769\& 'CHECK', # compiler phase
770\& );
771.Ve
772.PP
773Installing handlers into \s-1UNIVERSAL\s0, makes them...err..universal.
774For example:
775.PP
776.Vb 2
777\& package Descriptions;
778\& use Attribute::Handlers;
779.Ve
780.PP
781.Vb 2
782\& my %name;
783\& sub name { return $name{$_[2]}||*{$_[1]}{NAME} }
784.Ve
785.PP
786.Vb 3
787\& sub UNIVERSAL::Name :ATTR {
788\& $name{$_[2]} = $_[4];
789\& }
790.Ve
791.PP
792.Vb 3
793\& sub UNIVERSAL::Purpose :ATTR {
794\& print STDERR "Purpose of ", &name, " is $_[4]\en";
795\& }
796.Ve
797.PP
798.Vb 3
799\& sub UNIVERSAL::Unit :ATTR {
800\& print STDERR &name, " measured in $_[4]\en";
801\& }
802.Ve
803.PP
804Let's you write:
805.PP
806.Vb 1
807\& use Descriptions;
808.Ve
809.PP
810.Vb 3
811\& my $capacity : Name(capacity)
812\& : Purpose(to store max storage capacity for files)
813\& : Unit(Gb);
814.Ve
815.PP
816.Vb 1
817\& package Other;
818.Ve
819.PP
820.Vb 1
821\& sub foo : Purpose(to foo all data before barring it) { }
822.Ve
823.PP
824.Vb 1
825\& # etc.
826.Ve
827.SH "DIAGNOSTICS"
828.IX Header "DIAGNOSTICS"
829.ie n .IP """Bad attribute type: ATTR(%s)""" 4
830.el .IP "\f(CWBad attribute type: ATTR(%s)\fR" 4
831.IX Item "Bad attribute type: ATTR(%s)"
832An attribute handler was specified with an \f(CW\*(C`:ATTR(\f(CIref_type\f(CW)\*(C'\fR, but the
833type of referent it was defined to handle wasn't one of the five permitted:
834\&\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.
835.ie n .IP """Attribute handler %s doesn't handle %s attributes""" 4
836.el .IP "\f(CWAttribute handler %s doesn't handle %s attributes\fR" 4
837.IX Item "Attribute handler %s doesn't handle %s attributes"
838A handler for attributes of the specified name \fIwas\fR defined, but not
839for the specified type of declaration. Typically encountered whe trying
840to apply a \f(CW\*(C`VAR\*(C'\fR attribute handler to a subroutine, or a \f(CW\*(C`SCALAR\*(C'\fR
841attribute handler to some other type of variable.
842.ie n .IP """Declaration of %s attribute in package %s may clash with future reserved word""" 4
843.el .IP "\f(CWDeclaration of %s attribute in package %s may clash with future reserved word\fR" 4
844.IX Item "Declaration of %s attribute in package %s may clash with future reserved word"
845A handler for an attributes with an all-lowercase name was declared. An
846attribute with an all-lowercase name might have a meaning to Perl
847itself some day, even though most don't yet. Use a mixed-case attribute
848name, instead.
849.ie n .IP """Can't have two ATTR specifiers on one subroutine""" 4
850.el .IP "\f(CWCan't have two ATTR specifiers on one subroutine\fR" 4
851.IX Item "Can't have two ATTR specifiers on one subroutine"
852You just can't, okay?
853Instead, put all the specifications together with commas between them
854in a single \f(CW\*(C`ATTR(\f(CIspecification\f(CW)\*(C'\fR.
855.ie n .IP """Can't autotie a %s""" 4
856.el .IP "\f(CWCan't autotie a %s\fR" 4
857.IX Item "Can't autotie a %s"
858You can only declare autoties for types \f(CW"SCALAR"\fR, \f(CW"ARRAY"\fR, and
859\&\f(CW"HASH"\fR. They're the only things (apart from typeglobs \*(-- which are
860not declarable) that Perl can tie.
861.ie n .IP """Internal error: %s symbol went missing""" 4
862.el .IP "\f(CWInternal error: %s symbol went missing\fR" 4
863.IX Item "Internal error: %s symbol went missing"
864Something is rotten in the state of the program. An attributed
865subroutine ceased to exist between the point it was declared and the point
866at which its attribute handler(s) would have been called.
867.ie n .IP """Won't be able to apply END handler""" 4
868.el .IP "\f(CWWon't be able to apply END handler\fR" 4
869.IX Item "Won't be able to apply END handler"
870You have defined an \s-1END\s0 handler for an attribute that is being applied
871to a lexical variable. Since the variable may not be available during \s-1END\s0
872this won't happen.
873.SH "AUTHOR"
874.IX Header "AUTHOR"
875Damian Conway (damian@conway.org)
876.SH "BUGS"
877.IX Header "BUGS"
878There are undoubtedly serious bugs lurking somewhere in code this funky :\-)
879Bug reports and other feedback are most welcome.
880.SH "COPYRIGHT"
881.IX Header "COPYRIGHT"
882.Vb 3
883\& Copyright (c) 2001, Damian Conway. All Rights Reserved.
884\& This module is free software. It may be used, redistributed
885\& and/or modified under the same terms as Perl itself.
886.Ve