# perldoc > Attribute::Handlers

---
type: CommandReference
command: Attribute::Handlers
mode: perldoc
section: ""
source: perldoc
---

## Quick Reference

- `sub Handler :ATTR(SCALAR) { ... }` — define a handler for SCALAR attributes
- `sub Handler :ATTR(ARRAY) { ... }` — define a handler for ARRAY attributes
- `sub Handler :ATTR(HASH) { ... }` — define a handler for HASH attributes
- `sub Handler :ATTR(CODE) { ... }` — define a handler for CODE attributes
- `sub Handler :ATTR(ANY) { ... }` — define a handler for any type (equivalent to `:ATTR`)
- `sub Handler :ATTR(SCALAR,BEGIN) { ... }` — phase-specific handler (BEGIN, CHECK, INIT, END)
- `use Attribute::Handlers autotie => { AttrName => 'Tie::Class' };` — automatically tie variables with an attribute
- `my $sym = Attribute::Handlers::findsym($package, $referent);` — lookup symbol table entry for a referent

## Name

Attribute::Handlers - Simpler definition of attribute handlers

## Synopsis

perl
package MyClass;
require 5.006;
use Attribute::Handlers;
no warnings 'redefine';

sub Good :ATTR(SCALAR) {
    my ($package, $symbol, $referent, $attr, $data) = @_;
    # invoked for scalar variables with :Good attribute
}

sub Good :ATTR(ARRAY) { ... }
sub Good :ATTR(HASH) { ... }
sub Ugly :ATTR(CODE) { ... }
sub Omni :ATTR { ... }   # handles any type, use ref($_[2]) to determine type

use Attribute::Handlers autotie => { Cycle => 'Tie::Cycle' };
my $next : Cycle(['A'..'Z']);
## Options

- `ATTR(SCALAR|ARRAY|HASH|CODE|ANY)` — declare the type of variable the handler applies to. Without arguments, `:ATTR` is equivalent to `:ATTR(ANY)`.
- `ATTR(RAWDATA)` — disable automatic interpretation of attribute data; pass raw string instead of array reference.
- `ATTR(SCALAR,RAWDATA)` — combine type and raw data specifiers.
- `ATTR(BEGIN|CHECK|INIT|END)` — specify compilation phase(s) when handler is called. Default is `CHECK`. Multiple phases allowed: `ATTR(SCALAR,BEGIN,END)`.
- `autotie => { AttributeName => 'Tie::Class' }` — automatically tie variables with the given attribute to the specified class. The attribute is installed in the current package unless qualified.
- `autotieref => { AttributeName => 'Tie::Class' }` — like `autotie` but passes an extra reference to the tied variable as the first argument to the `TIESCALAR`/`TIEHASH`/`TIEARRAY` method.
- `findsym($package, $referent)` — utility function to look up the typeglob for a referent in a package. Memoizes results.

## Examples

**Basic handler definition:**

perl
package LoudDecl;
use Attribute::Handlers;

sub Loud :ATTR {
    my ($package, $symbol, $referent, $attr, $data, $phase, $filename, $linenum) = @_;
    print STDERR ref($referent), " ", *{$symbol}{NAME}, " ascribed ${attr} with data ($data) in phase $phase\n";
}
**Typed lexical variable invoking a handler from the typed package:**

perl
package OtherClass;
my LoudDecl $loudobj : Loud;  # invokes LoudDecl::Loud handler
**Phase-specific handlers:**

perl
sub Early  :ATTR(SCALAR,BEGIN) { ... }
sub Normal :ATTR(SCALAR,CHECK) { ... }
sub Late   :ATTR(SCALAR,INIT)  { ... }
sub Final  :ATTR(SCALAR,END)   { ... }
**Using autotie to tie variables automatically:**

perl
use Attribute::Handlers autotie => { Cycle => 'Tie::Cycle' };
my $next : Cycle(['A'..'Z']);  # $next is tied to Tie::Cycle
**Using autotieref to pass reference to the tied variable:**

perl
use Attribute::Handlers autotieref => { Selfish => 'Tie::Selfish' };
my $var : Selfish(@args);  # effect: tie my $var, 'Tie::Selfish', \$var, @args
**Installing UNIVERSAL attributes for global use:**

perl
package Descriptions;
use Attribute::Handlers;
my %name;
sub UNIVERSAL::Name :ATTR { $name{$_[2]} = $_[4]; }
sub UNIVERSAL::Purpose :ATTR { print STDERR "Purpose: $_[4]\n"; }
## See Also

- [perlsub](https://perldoc.perl.org/perlsub) — Perl subroutines
- [attributes](https://perldoc.perl.org/attributes) — built-in attribute mechanism
- [Tie::Cycle](https://metacpan.org/pod/Tie::Cycle) — example tie class used in autotie
- [Tie::Dir](https://metacpan.org/pod/Tie::Dir) — another tie class example