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