package HandlerBing;
use Attribute::Handlers;
sub Nate1 :ATTR {
my(@attrs) = @_; # We simply want to test by dumping the attributes
print "attributes: \n",
join("\n", @attrs), "\n";
}
# true.
1;
This stub for the HandlerBing class creates a handler for the
attribute :Nate1. When you want to use this
handler while within HandlerBing, you can do the following:
sub Nate2 :Nate1 {
my (@stuff) = @_;
print STDERR "in Nate2 ", join("\n", @stuff), "\n";
}
When you call Nate2, it invokes the
Nate1 handler and passes the following elements
into the @_ array:
- 0
-
The name of the package in which the handler was declared
- 1
-
A reference to the symbol table entry (a typeglob) that contains the
subroutine
- 2
-
A reference to the subroutine
- 3
-
The name of the attribute itself
- 4
-
Any data associated with that attribute
- 5
-
The name of the compilation phase in which the handler was invoked
The same holds true for declaring any variables with the
:Nate1 attribute within HandlerBing:
my $monica :Nate1;
my $phoebe :Nate1;
Attribute::Handlers also supports typed lexicals. This is a nice
feature, since you can invoke a handler from a package that defines
another one from whatever package you're in (or
writing!). For example:
package MustNotSeeTV;
my HandlerBring $whatever : Nate1;
# true
1;
You can apply handlers only by type, if you wish. You can do this by
passing a built-in type to :ATTR:
package ReRun;
sub Tiresome :ATTR(HASH) { print "your reruns have become tiresome\n"; }
# true
1;
In the above code, Tiresome is an attribute
handler that applies only to HASH es. And
certainly, you can declare separate handlers (of the same names) for
the other types.
If you have problems passing certain data to a handler, such as
now..later or laugh@you, you
can use the RAWDATA type to pass this data
cleanly:
sub RawLikeSushi : ATTR(RAWDATA) { ... do something ... }