Attribute-Handlers

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN


        sub Good : ATTR(SCALAR) {
            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']);

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 "BEGIN", "CHECK", "INIT", or "END"
    block). ("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
    ":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 ":Loud" in the class LoudDecl.
    Thereafter, any subroutine declared with a ":Loud" attribute in the
    class LoudDecl:

        package LoudDecl;

        sub foo: Loud {...}

    causes the above handler to be invoked, and passed:

    [0] the name of the package into which it was declared;

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

    [2] a reference to the subroutine;

    [3] the name of the attribute;

    [4] any data associated with that attribute;

    [5] the name of the phase in which the handler is being invoked;

    [6] the filename in which the handler is being invoked;

    [7] the line number in this file.

    Likewise, declaring any variables with the ":Loud" attribute within the
    package:

        package LoudDecl;

        my $foo :Loud;
        my @foo :Loud;
        my %foo :Loud;

    will cause the handler to be called with a similar argument list
    (except, of course, that $_[2] will be a reference to the variable).

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

( run in 0.698 second using v1.00-cache-2.02-grep-82fe00e-cpan-2c419f77a38b )