| 1 |
package MyClass; |
| 2 |
$VERSION = '1.00'; |
| 3 |
use 5.006; |
| 4 |
use parent qw(Attribute::Handlers); |
| 5 |
no warnings 'redefine'; |
| 6 |
|
| 7 |
|
| 8 |
sub Good : ATTR(SCALAR) { |
| 9 |
my ($package, $symbol, $referent, $attr, $data) = @_; |
| 10 |
|
| 11 |
# Invoked for any scalar variable with a :Good attribute, |
| 12 |
# provided the variable was declared in MyClass (or |
| 13 |
# a derived class) or typed to MyClass. |
| 14 |
|
| 15 |
# Do whatever to $referent here (executed in CHECK phase). |
| 16 |
local $" = ", "; |
| 17 |
print "MyClass::Good:ATTR(SCALAR)(@_);\n"; |
| 18 |
}; |
| 19 |
|
| 20 |
sub Bad : ATTR(SCALAR) { |
| 21 |
# Invoked for any scalar variable with a :Bad attribute, |
| 22 |
# provided the variable was declared in MyClass (or |
| 23 |
# a derived class) or typed to MyClass. |
| 24 |
local $" = ", "; |
| 25 |
print "MyClass::Bad:ATTR(SCALAR)(@_);\n"; |
| 26 |
} |
| 27 |
|
| 28 |
sub Good : ATTR(ARRAY) { |
| 29 |
# Invoked for any array variable with a :Good attribute, |
| 30 |
# provided the variable was declared in MyClass (or |
| 31 |
# a derived class) or typed to MyClass. |
| 32 |
local $" = ", "; |
| 33 |
print "MyClass::Good:ATTR(ARRAY)(@_);\n"; |
| 34 |
}; |
| 35 |
|
| 36 |
sub Good : ATTR(HASH) { |
| 37 |
# Invoked for any hash variable with a :Good attribute, |
| 38 |
# provided the variable was declared in MyClass (or |
| 39 |
# a derived class) or typed to MyClass. |
| 40 |
local $" = ", "; |
| 41 |
print "MyClass::Good:ATTR(HASH)(@_);\n"; |
| 42 |
}; |
| 43 |
|
| 44 |
sub Ugly : ATTR(CODE) { |
| 45 |
# Invoked for any subroutine declared in MyClass (or a |
| 46 |
# derived class) with an :Ugly attribute. |
| 47 |
local $" = ", "; |
| 48 |
print "MyClass::UGLY:ATTR(CODE)(@_);\n"; |
| 49 |
}; |
| 50 |
|
| 51 |
sub Omni : ATTR { |
| 52 |
# Invoked for any scalar, array, hash, or subroutine |
| 53 |
# with an :Omni attribute, provided the variable or |
| 54 |
# subroutine was declared in MyClass (or a derived class) |
| 55 |
# or the variable was typed to MyClass. |
| 56 |
# Use ref($_[2]) to determine what kind of referent it was. |
| 57 |
local $" = ", "; |
| 58 |
my $type = ref $_[2]; |
| 59 |
print "MyClass::OMNI:ATTR($type)(@_);\n"; |
| 60 |
use Data::Dumper 'Dumper'; |
| 61 |
print Dumper [ \@_ ]; |
| 62 |
}; |
| 63 |
|
| 64 |
1; |