1 ==============================================================================
3 ==============================================================================
7 Attribute::Handlers - Simpler definition of attribute handlers
10 This document describes version 0.79 of Attribute::Handlers, released
16 use Attribute::Handlers;
17 no warnings 'redefine';
20 sub Good : ATTR(SCALAR) {
21 my ($package, $symbol, $referent, $attr, $data) = @_;
23 # Invoked for any scalar variable with a :Good attribute,
24 # provided the variable was declared in MyClass (or
25 # a derived class) or typed to MyClass.
27 # Do whatever to $referent here (executed in CHECK phase).
31 sub Bad : ATTR(SCALAR) {
32 # Invoked for any scalar variable with a :Bad attribute,
33 # provided the variable was declared in MyClass (or
34 # a derived class) or typed to MyClass.
38 sub Good : ATTR(ARRAY) {
39 # Invoked for any array variable with a :Good attribute,
40 # provided the variable was declared in MyClass (or
41 # a derived class) or typed to MyClass.
45 sub Good : ATTR(HASH) {
46 # Invoked for any hash variable with a :Good attribute,
47 # provided the variable was declared in MyClass (or
48 # a derived class) or typed to MyClass.
52 sub Ugly : ATTR(CODE) {
53 # Invoked for any subroutine declared in MyClass (or a
54 # derived class) with an :Ugly attribute.
59 # Invoked for any scalar, array, hash, or subroutine
60 # with an :Omni attribute, provided the variable or
61 # subroutine was declared in MyClass (or a derived class)
62 # or the variable was typed to MyClass.
63 # Use ref($_[2]) to determine what kind of referent it was.
68 use Attribute::Handlers autotie => { Cycle => Tie::Cycle };
70 my $next : Cycle(['A'..'Z']);
73 This module, when inherited by a package, allows that package's class to
74 define attribute handler subroutines for specific attributes. Variables
75 and subroutines subsequently defined in that package, or in packages
76 derived from that package may be given attributes with the same names as
77 the attribute handler subroutines, which will then be called in one of
78 the compilation phases (i.e. in a "BEGIN", "CHECK", "INIT", or "END"
79 block). ("UNITCHECK" blocks don't correspond to a global compilation
80 phase, so they can't be specified here.)
82 To create a handler, define it as a subroutine with the same name as the
83 desired attribute, and declare the subroutine itself with the attribute
87 use Attribute::Handlers;
90 my ($package, $symbol, $referent, $attr, $data, $phase, $filename, $linenum) = @_;
93 *{$symbol}{NAME}, " ",
94 "($referent) ", "was just declared ",
95 "and ascribed the ${attr} attribute ",
96 "with data ($data)\n",
98 "in file $filename at line $linenum\n";
101 This creates a handler for the attribute ":Loud" in the class LoudDecl.
102 Thereafter, any subroutine declared with a ":Loud" attribute in the
109 causes the above handler to be invoked, and passed:
111 [0] the name of the package into which it was declared;
113 [1] a reference to the symbol table entry (typeglob) containing the
116 [2] a reference to the subroutine;
118 [3] the name of the attribute;
120 [4] any data associated with that attribute;
122 [5] the name of the phase in which the handler is being invoked;
124 [6] the filename in which the handler is being invoked;
126 [7] the line number in this file.
128 Likewise, declaring any variables with the ":Loud" attribute within the
137 will cause the handler to be called with a similar argument list
138 (except, of course, that $_[2] will be a reference to the variable).
140 The package name argument will typically be the name of the class into
141 which the subroutine was declared, but it may also be the name of a
142 derived class (since handlers are inherited).
144 If a lexical variable is given an attribute, there is no symbol table to
145 which it belongs, so the symbol table argument ($_[1]) is set to the
146 string 'LEXICAL' in that case. Likewise, ascribing an attribute to an
147 anonymous subroutine results in a symbol table argument of 'ANON'.
149 The data argument passes in the value (if any) associated with the
150 attribute. For example, if &foo had been declared:
152 sub foo :Loud("turn it up to 11, man!") {...}
154 then a reference to an array containing the string "turn it up to 11,
155 man!" would be passed as the last argument.
157 Attribute::Handlers makes strenuous efforts to convert the data argument
158 ($_[4]) to a useable form before passing it to the handler (but see
159 "Non-interpretive attribute handlers"). If those efforts succeed, the
160 interpreted data is passed in an array reference; if they fail, the raw
161 data is passed as a string. For example, all of these:
163 sub foo :Loud(till=>ears=>are=>bleeding) {...}
164 sub foo :Loud(qw/till ears are bleeding/) {...}
165 sub foo :Loud(qw/my, ears, are, bleeding/) {...}
166 sub foo :Loud(till,ears,are,bleeding) {...}
168 causes it to pass "['till','ears','are','bleeding']" as the handler's
169 data argument. While:
171 sub foo :Loud(['till','ears','are','bleeding']) {...}
173 causes it to pass "[ ['till','ears','are','bleeding'] ]"; the array
174 reference specified in the data being passed inside the standard array
175 reference indicating successful interpretation.
177 However, if the data can't be parsed as valid Perl, then it is passed as
178 an uninterpreted string. For example:
180 sub foo :Loud(my,ears,are,bleeding) {...}
181 sub foo :Loud(qw/my ears are bleeding) {...}
183 cause the strings 'my,ears,are,bleeding' and 'qw/my ears are bleeding'
184 respectively to be passed as the data argument.
186 If no value is associated with the attribute, "undef" is passed.
189 Regardless of the package in which it is declared, if a lexical variable
190 is ascribed an attribute, the handler that is invoked is the one
191 belonging to the package to which it is typed. For example, the
192 following declarations:
196 my LoudDecl $loudobj : Loud;
197 my LoudDecl @loudobjs : Loud;
198 my LoudDecl %loudobjex : Loud;
200 causes the LoudDecl::Loud handler to be invoked (even if OtherClass also
201 defines a handler for ":Loud" attributes).
203 Type-specific attribute handlers
204 If an attribute handler is declared and the ":ATTR" specifier is given
205 the name of a built-in type ("SCALAR", "ARRAY", "HASH", or "CODE"), the
206 handler is only applied to declarations of that type. For example, the
207 following definition:
211 sub RealLoud :ATTR(SCALAR) { print "Yeeeeow!" }
213 creates an attribute handler that applies only to scalars:
218 my $metal : RealLoud; # invokes &LoudDecl::RealLoud
219 my @metal : RealLoud; # error: unknown attribute
220 my %metal : RealLoud; # error: unknown attribute
221 sub metal : RealLoud {...} # error: unknown attribute
223 You can, of course, declare separate handlers for these types as well
224 (but you'll need to specify "no warnings 'redefine'" to do it quietly):
227 use Attribute::Handlers;
228 no warnings 'redefine';
230 sub RealLoud :ATTR(SCALAR) { print "Yeeeeow!" }
231 sub RealLoud :ATTR(ARRAY) { print "Urrrrrrrrrr!" }
232 sub RealLoud :ATTR(HASH) { print "Arrrrrgggghhhhhh!" }
233 sub RealLoud :ATTR(CODE) { croak "Real loud sub torpedoed" }
235 You can also explicitly indicate that a single handler is meant to be
236 used for all types of referents like so:
239 use Attribute::Handlers;
241 sub SeriousLoud :ATTR(ANY) { warn "Hearing loss imminent" }
243 (I.e. "ATTR(ANY)" is a synonym for ":ATTR").
245 Non-interpretive attribute handlers
246 Occasionally the strenuous efforts Attribute::Handlers makes to convert
247 the data argument ($_[4]) to a useable form before passing it to the
248 handler get in the way.
250 You can turn off that eagerness-to-help by declaring an attribute
251 handler with the keyword "RAWDATA". For example:
253 sub Raw : ATTR(RAWDATA) {...}
254 sub Nekkid : ATTR(SCALAR,RAWDATA) {...}
255 sub Au::Naturale : ATTR(RAWDATA,ANY) {...}
257 Then the handler makes absolutely no attempt to interpret the data it
258 receives and simply passes it as a string:
260 my $power : Raw(1..100); # handlers receives "1..100"
262 Phase-specific attribute handlers
263 By default, attribute handlers are called at the end of the compilation
264 phase (in a "CHECK" block). This seems to be optimal in most cases
265 because most things that can be defined are defined by that point but
266 nothing has been executed.
268 However, it is possible to set up attribute handlers that are called at
269 other points in the program's compilation or execution, by explicitly
270 stating the phase (or phases) in which you wish the attribute handler to
271 be called. For example:
273 sub Early :ATTR(SCALAR,BEGIN) {...}
274 sub Normal :ATTR(SCALAR,CHECK) {...}
275 sub Late :ATTR(SCALAR,INIT) {...}
276 sub Final :ATTR(SCALAR,END) {...}
277 sub Bookends :ATTR(SCALAR,BEGIN,END) {...}
279 As the last example indicates, a handler may be set up to be (re)called
280 in two or more phases. The phase name is passed as the handler's final
283 Note that attribute handlers that are scheduled for the "BEGIN" phase
284 are handled as soon as the attribute is detected (i.e. before any
285 subsequently defined "BEGIN" blocks are executed).
287 Attributes as "tie" interfaces
288 Attributes make an excellent and intuitive interface through which to
289 tie variables. For example:
291 use Attribute::Handlers;
294 sub UNIVERSAL::Cycle : ATTR(SCALAR) {
295 my ($package, $symbol, $referent, $attr, $data, $phase) = @_;
296 $data = [ $data ] unless ref $data eq 'ARRAY';
297 tie $$referent, 'Tie::Cycle', $data;
304 my $next : Cycle('A'..'Z'); # $next is now a tied variable
310 Note that, because the "Cycle" attribute receives its arguments in the
311 $data variable, if the attribute is given a list of arguments, $data
312 will consist of a single array reference; otherwise, it will consist of
313 the single argument directly. Since Tie::Cycle requires its cycling
314 values to be passed as an array reference, this means that we need to
315 wrap non-array-reference arguments in an array constructor:
317 $data = [ $data ] unless ref $data eq 'ARRAY';
319 Typically, however, things are the other way around: the tieable class
320 expects its arguments as a flattened list, so the attribute looks like:
322 sub UNIVERSAL::Cycle : ATTR(SCALAR) {
323 my ($package, $symbol, $referent, $attr, $data, $phase) = @_;
324 my @data = ref $data eq 'ARRAY' ? @$data : $data;
325 tie $$referent, 'Tie::Whatever', @data;
328 This software pattern is so widely applicable that Attribute::Handlers
329 provides a way to automate it: specifying 'autotie' in the "use
330 Attribute::Handlers" statement. So, the cycling example, could also be
333 use Attribute::Handlers autotie => { Cycle => 'Tie::Cycle' };
339 my $next : Cycle(['A'..'Z']); # $next is now a tied variable
344 Note that we now have to pass the cycling values as an array reference,
345 since the "autotie" mechanism passes "tie" a list of arguments as a list
346 (as in the Tie::Whatever example), *not* as an array reference (as in
347 the original Tie::Cycle example at the start of this section).
349 The argument after 'autotie' is a reference to a hash in which each key
350 is the name of an attribute to be created, and each value is the class
351 to which variables ascribed that attribute should be tied.
353 Note that there is no longer any need to import the Tie::Cycle module --
354 Attribute::Handlers takes care of that automagically. You can even pass
355 arguments to the module's "import" subroutine, by appending them to the
356 class name. For example:
358 use Attribute::Handlers
359 autotie => { Dir => 'Tie::Dir qw(DIR_UNLINK)' };
361 If the attribute name is unqualified, the attribute is installed in the
362 current package. Otherwise it is installed in the qualifier's package:
366 use Attribute::Handlers autotie => {
367 Other::Good => Tie::SecureHash, # tie attr installed in Other::
368 Bad => Tie::Taxes, # tie attr installed in Here::
369 UNIVERSAL::Ugly => Software::Patent # tie attr installed everywhere
372 Autoties are most commonly used in the module to which they actually
373 tie, and need to export their attributes to any module that calls them.
374 To facilitate this, Attribute::Handlers recognizes a special
375 "pseudo-class" -- "__CALLER__", which may be specified as the qualifier
378 package Tie::Me::Kangaroo:Down::Sport;
380 use Attribute::Handlers autotie => { '__CALLER__::Roo' => __PACKAGE__ };
382 This causes Attribute::Handlers to define the "Roo" attribute in the
383 package that imports the Tie::Me::Kangaroo:Down::Sport module.
385 Note that it is important to quote the __CALLER__::Roo identifier
386 because a bug in perl 5.8 will refuse to parse it and cause an unknown
389 Passing the tied object to "tie"
390 Occasionally it is important to pass a reference to the object being
391 tied to the TIESCALAR, TIEHASH, etc. that ties it.
393 The "autotie" mechanism supports this too. The following code:
395 use Attribute::Handlers autotieref => { Selfish => Tie::Selfish };
396 my $var : Selfish(@args);
398 has the same effect as:
400 tie my $var, 'Tie::Selfish', @args;
402 But when "autotieref" is used instead of "autotie":
404 use Attribute::Handlers autotieref => { Selfish => Tie::Selfish };
405 my $var : Selfish(@args);
407 the effect is to pass the "tie" call an extra reference to the variable
410 tie my $var, 'Tie::Selfish', \$var, @args;
413 If the class shown in SYNOPSIS were placed in the MyClass.pm module,
414 then the following code:
419 my MyClass $slr :Good :Bad(1**1-1) :Omni(-vorous);
421 package SomeOtherClass;
426 sub fn :Ugly(sister) :Omni('po',tent()) {...}
427 my @arr :Good :Omni(s/cie/nt/);
428 my %hsh :Good(q/bye/) :Omni(q/bus/);
430 would cause the following handlers to be invoked:
432 # my MyClass $slr :Good :Bad(1**1-1) :Omni(-vorous);
434 MyClass::Good:ATTR(SCALAR)( 'MyClass', # class
435 'LEXICAL', # no typeglob
439 'CHECK', # compiler phase
442 MyClass::Bad:ATTR(SCALAR)( 'MyClass', # class
443 'LEXICAL', # no typeglob
447 'CHECK', # compiler phase
450 MyClass::Omni:ATTR(SCALAR)( 'MyClass', # class
451 'LEXICAL', # no typeglob
454 '-vorous' # eval'd attr data
455 'CHECK', # compiler phase
459 # sub fn :Ugly(sister) :Omni('po',tent()) {...}
461 MyClass::UGLY:ATTR(CODE)( 'SomeOtherClass', # class
462 \*SomeOtherClass::fn, # typeglob
463 \&SomeOtherClass::fn, # referent
465 'sister' # eval'd attr data
466 'CHECK', # compiler phase
469 MyClass::Omni:ATTR(CODE)( 'SomeOtherClass', # class
470 \*SomeOtherClass::fn, # typeglob
471 \&SomeOtherClass::fn, # referent
473 ['po','acle'] # eval'd attr data
474 'CHECK', # compiler phase
478 # my @arr :Good :Omni(s/cie/nt/);
480 MyClass::Good:ATTR(ARRAY)( 'SomeOtherClass', # class
481 'LEXICAL', # no typeglob
485 'CHECK', # compiler phase
488 MyClass::Omni:ATTR(ARRAY)( 'SomeOtherClass', # class
489 'LEXICAL', # no typeglob
492 "" # eval'd attr data
493 'CHECK', # compiler phase
497 # my %hsh :Good(q/bye) :Omni(q/bus/);
499 MyClass::Good:ATTR(HASH)( 'SomeOtherClass', # class
500 'LEXICAL', # no typeglob
503 'q/bye' # raw attr data
504 'CHECK', # compiler phase
507 MyClass::Omni:ATTR(HASH)( 'SomeOtherClass', # class
508 'LEXICAL', # no typeglob
511 'bus' # eval'd attr data
512 'CHECK', # compiler phase
515 Installing handlers into UNIVERSAL, makes them...err..universal. For
518 package Descriptions;
519 use Attribute::Handlers;
522 sub name { return $name{$_[2]}||*{$_[1]}{NAME} }
524 sub UNIVERSAL::Name :ATTR {
525 $name{$_[2]} = $_[4];
528 sub UNIVERSAL::Purpose :ATTR {
529 print STDERR "Purpose of ", &name, " is $_[4]\n";
532 sub UNIVERSAL::Unit :ATTR {
533 print STDERR &name, " measured in $_[4]\n";
540 my $capacity : Name(capacity)
541 : Purpose(to store max storage capacity for files)
547 sub foo : Purpose(to foo all data before barring it) { }
552 "Bad attribute type: ATTR(%s)"
553 An attribute handler was specified with an ":ATTR(*ref_type*)", but
554 the type of referent it was defined to handle wasn't one of the five
555 permitted: "SCALAR", "ARRAY", "HASH", "CODE", or "ANY".
557 "Attribute handler %s doesn't handle %s attributes"
558 A handler for attributes of the specified name *was* defined, but
559 not for the specified type of declaration. Typically encountered whe
560 trying to apply a "VAR" attribute handler to a subroutine, or a
561 "SCALAR" attribute handler to some other type of variable.
563 "Declaration of %s attribute in package %s may clash with future
565 A handler for an attributes with an all-lowercase name was declared.
566 An attribute with an all-lowercase name might have a meaning to Perl
567 itself some day, even though most don't yet. Use a mixed-case
568 attribute name, instead.
570 "Can't have two ATTR specifiers on one subroutine"
571 You just can't, okay? Instead, put all the specifications together
572 with commas between them in a single "ATTR(*specification*)".
575 You can only declare autoties for types "SCALAR", "ARRAY", and
576 "HASH". They're the only things (apart from typeglobs -- which are
577 not declarable) that Perl can tie.
579 "Internal error: %s symbol went missing"
580 Something is rotten in the state of the program. An attributed
581 subroutine ceased to exist between the point it was declared and the
582 point at which its attribute handler(s) would have been called.
584 "Won't be able to apply END handler"
585 You have defined an END handler for an attribute that is being
586 applied to a lexical variable. Since the variable may not be
587 available during END this won't happen.
590 Damian Conway (damian@conway.org). The maintainer of this module is now
591 Rafael Garcia-Suarez (rgarciasuarez@gmail.com).
593 Maintainer of the CPAN release is Steffen Mueller (smueller@cpan.org).
594 Contact him with technical difficulties with respect to the packaging of
598 There are undoubtedly serious bugs lurking somewhere in code this funky
599 :-) Bug reports and other feedback are most welcome.
601 COPYRIGHT AND LICENSE
602 Copyright (c) 2001-2009, Damian Conway. All Rights Reserved.
603 This module is free software. It may be used, redistributed
604 and/or modified under the same terms as Perl itself.