Attribute-Handlers

 view release on metacpan or  search on metacpan

lib/Attribute/Handlers.pm  view on Meta::CPAN

	my ($package, $symbol, $referent, $attr, $data) = @_;

	# Invoked for any scalar variable with a :Good attribute,
	# provided the variable was declared in MyClass (or
	# a derived class) or typed to MyClass.

	# Do whatever to $referent here (executed in CHECK phase).
	...
    }

    sub Bad : ATTR(SCALAR) {
	# Invoked for any scalar variable with a :Bad attribute,
	# provided the variable was declared in MyClass (or
	# a derived class) or typed to MyClass.
	...
    }

    sub Good : ATTR(ARRAY) {
	# Invoked for any array variable with a :Good attribute,
	# provided the variable was declared in MyClass (or
	# a derived class) or typed to MyClass.
	...
    }

    sub Good : ATTR(HASH) {
	# Invoked for any hash variable with a :Good attribute,
	# provided the variable was declared in MyClass (or
	# a derived class) or typed to MyClass.
	...
    }

    sub Ugly : ATTR(CODE) {
	# Invoked for any subroutine declared in MyClass (or a 
	# derived class) with an :Ugly attribute.
	...
    }

    sub Omni : ATTR {
	# Invoked for any scalar, array, hash, or subroutine
	# with an :Omni attribute, provided the variable or
	# subroutine was declared in MyClass (or a derived class)
	# or the variable was typed to MyClass.
	# Use ref($_[2]) to determine what kind of referent it was.
	...
    }


    use Attribute::Handlers autotie => { Cycle => Tie::Cycle };

    my $next : Cycle(['A'..'Z']);


=head1 DESCRIPTION

This module, when inherited by a package, allows that package's class to
define attribute handler subroutines for specific attributes. Variables
and subroutines subsequently defined in that package, or in packages
derived from that package may be given attributes with the same names as
the attribute handler subroutines, which will then be called in one of
the compilation phases (i.e. in a C<BEGIN>, C<CHECK>, C<INIT>, or C<END>
block). (C<UNITCHECK> blocks don't correspond to a global compilation
phase, so they can't be specified here.)

To create a handler, define it as a subroutine with the same name as
the desired attribute, and declare the subroutine itself with the  
attribute C<:ATTR>. For example:

    package LoudDecl;
    use Attribute::Handlers;

    sub Loud :ATTR {
	my ($package, $symbol, $referent, $attr, $data, $phase,
	    $filename, $linenum) = @_;
	print STDERR
	    ref($referent), " ",
	    *{$symbol}{NAME}, " ",
	    "($referent) ", "was just declared ",
	    "and ascribed the ${attr} attribute ",
	    "with data ($data)\n",
	    "in phase $phase\n",
	    "in file $filename at line $linenum\n";
    }

This creates a handler for the attribute C<:Loud> in the class LoudDecl.
Thereafter, any subroutine declared with a C<:Loud> attribute in the class
LoudDecl:

    package LoudDecl;

    sub foo: Loud {...}

causes the above handler to be invoked, and passed:

=over

=item [0]

the name of the package into which it was declared;

=item [1]

a reference to the symbol table entry (typeglob) containing the subroutine;

=item [2]

a reference to the subroutine;

=item [3]

the name of the attribute;

=item [4]

any data associated with that attribute;

=item [5]

the name of the phase in which the handler is being invoked;

=item [6]

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 4.376 seconds using v1.00-cache-2.02-grep-82fe00e-cpan-c30982ac1bc3 )