), the handler is only applied to declarations of that type. For example, the following definition: package LoudDecl; sub RealLoud :ATTR(SCALAR) { print "Yeeeeow!" } creates an attribute handler that applies only to scalars: package Painful; use base LoudDecl; my $metal : RealLoud; # invokes &LoudDecl::RealLoud my @metal : RealLoud; # error: unknown attribute my %metal : RealLoud; # error: unknown attribute sub metal : RealLoud {...} # error: unknown attribute You can, of course, declare separate handlers for these types as well (but you'll need to specify C to do it quietly): package LoudDecl; use Attribute::Handlers; no warnings 'redefine'; sub RealLoud :ATTR(SCALAR) { print "Yeeeeow!" } sub RealLoud :ATTR(ARRAY) { print "Urrrrrrrrrr!" } sub RealLoud :ATTR(HASH) { print "Arrrrrgggghhhhhh!" } sub RealLoud :ATTR(CODE) { croak "Real loud sub torpedoed" } You can also explicitly indicate that a single handler is meant to be used for all types of referents like so: package LoudDecl; use Attribute::Handlers; sub SeriousLoud :ATTR(ANY) { warn "Hearing loss imminent" } (I.e. C is a synonym for C<:ATTR>). =head2 Non-interpretive attribute handlers Occasionally the strenuous efforts Attribute::Handlers makes to convert the data argument (C<$_[4]>) to a usable form before passing it to the handler get in the way. You can turn off that eagerness-to-help by declaring an attribute handler with the keyword C. For example: sub Raw : ATTR(RAWDATA) {...} sub Nekkid : ATTR(SCALAR,RAWDATA) {...} sub Au::Naturale : ATTR(RAWDATA,ANY) {...} Then the handler makes absolutely no attempt to interpret the data it receives and simply passes it as a string: my $power : Raw(1..100); # handlers receives "1..100" =head2 Phase-specific attribute handlers By default, attribute handlers are called at the end of the compilation phase (in a C block). This seems to be optimal in most cases because most things that can be defined are defined by that point but nothing has been executed. However, it is possible to set up attribute handlers that are called at other points in the program's compilation or execution, by explicitly stating the phase (or phases) in which you wish the attribute handler to be called. For example: sub Early :ATTR(SCALAR,BEGIN) {...} sub Normal :ATTR(SCALAR,CHECK) {...} sub Late :ATTR(SCALAR,INIT) {...} sub Final :ATTR(SCALAR,END) {...} sub Bookends :ATTR(SCALAR,BEGIN,END) {...} As the last example indicates, a handler may be set up to be (re)called in two or more phases. The phase name is passed as the handler's final argument. Note that attribute handlers that are scheduled for the C phase are handled as soon as the attribute is detected (i.e. before any subsequently defined C blocks are executed). =head2 Attributes as C interfaces Attributes make an excellent and intuitive interface through which to tie variables. For example: use Attribute::Handlers; use Tie::Cycle; sub UNIVERSAL::Cycle : ATTR(SCALAR) { my ($package, $symbol, $referent, $attr, $data, $phase) = @_; $data = [ $data ] unless ref $data eq 'ARRAY'; tie $$referent, 'Tie::Cycle', $data; } # and thereafter... package main; my $next : Cycle('A'..'Z'); # $next is now a tied variable while (<>) { print $next; } Note that, because the C attribute receives its arguments in the C<$data> variable, if the attribute is given a list of arguments, C<$data> will consist of a single array reference; otherwise, it will consist of the single argument directly. Since Tie::Cycle requires its cycling values to be passed as an array reference, this means that we need to wrap non-array-reference arguments in an array constructor: $data = [ $data ] unless ref $data eq 'ARRAY'; Typically, however, things are the other way around: the tieable class expects its arguments as a flattened list, so the attribute looks like: sub UNIVERSAL::Cycle : ATTR(SCALAR) { my ($package, $symbol, $referent, $attr, $data, $phase) = @_; my @data = ref $data eq 'ARRAY' ? @$data : $data; tie $$referent, 'Tie::Whatever', @data; } This software pattern is so widely applicable that Attribute::Handlers provides a way to automate it: specifying C<'autotie'> in the C statement. So, the cycling example, could also be written: use Attribute::Handlers autotie => { Cycle => 'Tie::Cycle' }; # and thereafter... package main; my $next : Cycle(['A'..'Z']); # $next is now a tied variable while (<>) { print $next; } Note that we now have to pass the cycling values as an array reference, since the C mechanism passes C a list of arguments as a list (as in the Tie::Whatever example), I as an array reference (as in the original Tie::Cycle example at the start of this section). The argument after C<'autotie'> is a reference to a hash in which each key is the name of an attribute to be created, and each value is the class to which variables ascribed that attribute should be tied. Note that there is no longer any need to import the Tie::Cycle module -- Attribute::Handlers takes care of that automagically. You can even pass arguments to the module's C subroutine, by appending them to the class name. For example: use Attribute::Handlers autotie => { Dir => 'Tie::Dir qw(DIR_UNLINK)' }; If the attribute name is unqualified, the attribute is installed in the current package. Otherwise it is installed in the qualifier's package: package Here; use Attribute::Handlers autotie => { Other::Good => Tie::SecureHash, # tie attr installed in Other:: Bad => Tie::Taxes, # tie attr installed in Here:: UNIVERSAL::Ugly => Software::Patent # tie attr installed everywhere }; Autoties are most commonly used in the module to which they actually tie, and need to export their attributes to any module that calls them. To facilitate this, Attribute::Handlers recognizes a special "pseudo-class" -- C<__CALLER__>, which may be specified as the qualifier of an attribute: package Tie::Me::Kangaroo:Down::Sport; use Attribute::Handlers autotie => { '__CALLER__::Roo' => __PACKAGE__ }; This causes Attribute::Handlers to define the C attribute in the package that imports the Tie::Me::Kangaroo:Down::Sport module. Note that it is important to quote the __CALLER__::Roo identifier because a bug in perl 5.8 will refuse to parse it and cause an unknown error. =head3 Passing the tied object to C Occasionally it is important to pass a reference to the object being tied to the TIESCALAR, TIEHASH, etc. that ties it. The C mechanism supports this too. The following code: use Attribute::Handlers autotieref => { Selfish => Tie::Selfish }; my $var : Selfish(@args); has the same effect as: tie my $var, 'Tie::Selfish', @args; But when C<"autotieref"> is used instead of C<"autotie">: use Attribute::Handlers autotieref => { Selfish => Tie::Selfish }; my $var : Selfish(@args); the effect is to pass the C call an extra reference to the variable being tied: tie my $var, 'Tie::Selfish', \$var, @args; =head1 EXAMPLES If the class shown in L were placed in the MyClass.pm module, then the following code: package main; use MyClass; my MyClass $slr :Good :Bad(1**1-1) :Omni(-vorous); package SomeOtherClass; use base MyClass; sub tent { 'acle' } sub fn :Ugly(sister) :Omni('po',tent()) {...} my @arr :Good :Omni(s/cie/nt/); my %hsh :Good(q/bye/) :Omni(q/bus/); would cause the following handlers to be invoked: # my MyClass $slr :Good :Bad(1**1-1) :Omni(-vorous); MyClass::Good:ATTR(SCALAR)( 'MyClass', # class 'LEXICAL', # no typeglob \$slr, # referent 'Good', # attr name undef # no attr data 'CHECK', # compiler phase ); MyClass::Bad:ATTR(SCALAR)( 'MyClass', # class 'LEXICAL', # no typeglob \$slr, # referent 'Bad', # attr name 0 # eval'd attr data 'CHECK', # compiler phase ); MyClass::Omni:ATTR(SCALAR)( 'MyClass', # class 'LEXICAL', # no typeglob \$slr, # referent 'Omni', # attr name '-vorous' # eval'd attr data 'CHECK', # compiler phase ); # sub fn :Ugly(sister) :Omni('po',tent()) {...} MyClass::UGLY:ATTR(CODE)( 'SomeOtherClass', # class \*SomeOtherClass::fn, # typeglob \&SomeOtherClass::fn, # referent 'Ugly', # attr name 'sister' # eval'd attr data 'CHECK', # compiler phase ); MyClass::Omni:ATTR(CODE)( 'SomeOtherClass', # class \*SomeOtherClass::fn, # typeglob \&SomeOtherClass::fn, # referent 'Omni', # attr name ['po','acle'] # eval'd attr data 'CHECK', # compiler phase ); # my @arr :Good :Omni(s/cie/nt/); MyClass::Good:ATTR(ARRAY)( 'SomeOtherClass', # class 'LEXICAL', # no typeglob \@arr, # referent 'Good', # attr name undef # no attr data 'CHECK', # compiler phase ); MyClass::Omni:ATTR(ARRAY)( 'SomeOtherClass', # class 'LEXICAL', # no typeglob \@arr, # referent 'Omni', # attr name "" # eval'd attr data 'CHECK', # compiler phase ); # my %hsh :Good(q/bye) :Omni(q/bus/); MyClass::Good:ATTR(HASH)( 'SomeOtherClass', # class 'LEXICAL', # no typeglob \%hsh, # referent 'Good', # attr name 'q/bye' # raw attr data 'CHECK', # compiler phase ); MyClass::Omni:ATTR(HASH)( 'SomeOtherClass', # class 'LEXICAL', # no typeglob \%hsh, # referent 'Omni', # attr name 'bus' # eval'd attr data 'CHECK', # compiler phase ); Installing handlers into UNIVERSAL, makes them...err..universal. For example: package Descriptions; use Attribute::Handlers; my %name; sub name { return $name{$_[2]}||*{$_[1]}{NAME} } sub UNIVERSAL::Name :ATTR { $name{$_[2]} = $_[4]; } sub UNIVERSAL::Purpose :ATTR { print STDERR "Purpose of ", &name, " is $_[4]\n"; } sub UNIVERSAL::Unit :ATTR { print STDERR &name, " measured in $_[4]\n"; } Let's you write: use Descriptions; my $capacity : Name(capacity) : Purpose(to store max storage capacity for files) : Unit(Gb); package Other; sub foo : Purpose(to foo all data before barring it) { } # etc. =head1 UTILITY FUNCTIONS This module offers a single utility function, C. =over 4 =item findsym my $symbol = Attribute::Handlers::findsym($package, $referent); The function looks in the symbol table of C<$package> for the typeglob for C<$referent>, which is a reference to a variable or subroutine (SCALAR, ARRAY, HASH, or CODE). If it finds the typeglob, it returns it. Otherwise, it returns undef. Note that C memoizes the typeglobs it has previously successfully found, so subsequent calls with the same arguments should be much faster. =back =head1 DIAGNOSTICS =over =item C An attribute handler was specified with an C<:ATTR(I)>, but the type of referent it was defined to handle wasn't one of the five permitted: C, C, C, C, or C. =item C A handler for attributes of the specified name I defined, but not for the specified type of declaration. Typically encountered when trying to apply a C attribute handler to a subroutine, or a C attribute handler to some other type of variable. =item C A handler for an attributes with an all-lowercase name was declared. An attribute with an all-lowercase name might have a meaning to Perl itself some day, even though most don't yet. Use a mixed-case attribute name, instead. =item C You just can't, okay? Instead, put all the specifications together with commas between them in a single C)>. =item C You can only declare autoties for types C<"SCALAR">, C<"ARRAY">, and C<"HASH">. They're the only things (apart from typeglobs -- which are not declarable) that Perl can tie. =item C Something is rotten in the state of the program. An attributed subroutine ceased to exist between the point it was declared and the point at which its attribute handler(s) would have been called. =item C You have defined an END handler for an attribute that is being applied to a lexical variable. Since the variable may not be available during END this won't happen. =back =head1 AUTHOR Damian Conway (damian@conway.org). The maintainer of this module is now Rafael Garcia-Suarez (rgarciasuarez@gmail.com). Maintainer of the CPAN release is Steffen Mueller (smueller@cpan.org). Contact him with technical difficulties with respect to the packaging of the CPAN module. =head1 BUGS There are undoubtedly serious bugs lurking somewhere in code this funky :-) Bug reports and other feedback are most welcome. =head1 COPYRIGHT AND LICENSE Copyright (c) 2001-2014, Damian Conway. All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.
, or C. =item C A handler for attributes of the specified name I defined, but not for the specified type of declaration. Typically encountered when trying to apply a C attribute handler to a subroutine, or a C attribute handler to some other type of variable. =item C A handler for an attributes with an all-lowercase name was declared. An attribute with an all-lowercase name might have a meaning to Perl itself some day, even though most don't yet. Use a mixed-case attribute name, instead. =item C You just can't, okay? Instead, put all the specifications together with commas between them in a single C)>. =item C You can only declare autoties for types C<"SCALAR">, C<"ARRAY">, and C<"HASH">. They're the only things (apart from typeglobs -- which are not declarable) that Perl can tie. =item C Something is rotten in the state of the program. An attributed subroutine ceased to exist between the point it was declared and the point at which its attribute handler(s) would have been called. =item C You have defined an END handler for an attribute that is being applied to a lexical variable. Since the variable may not be available during END this won't happen. =back =head1 AUTHOR Damian Conway (damian@conway.org). The maintainer of this module is now Rafael Garcia-Suarez (rgarciasuarez@gmail.com). Maintainer of the CPAN release is Steffen Mueller (smueller@cpan.org). Contact him with technical difficulties with respect to the packaging of the CPAN module. =head1 BUGS There are undoubtedly serious bugs lurking somewhere in code this funky :-) Bug reports and other feedback are most welcome. =head1 COPYRIGHT AND LICENSE Copyright (c) 2001-2014, Damian Conway. All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.