Skip to content

Instantly share code, notes, and snippets.

@breezhang
Created April 16, 2014 13:44
Show Gist options
  • Save breezhang/10877624 to your computer and use it in GitHub Desktop.
Save breezhang/10877624 to your computer and use it in GitHub Desktop.
perl AOP attribute anothor ....
use strict;
use warnings;
{
package tools;
use Data::Dumper;
use Hook::LexWrap;
use Attribute::Handlers;
sub UNIVERSAL::Debug :ATTR(CODE) {
wrap $_[1], pre => sub { pop; print Dumper({'Pre' => \@_ })},
post => sub { pop; print Dumper({'Post' => \@_ })},
}
sub UNIVERSAL::Method :ATTR(CODE) {
my ($package, $symbol, $referent, $attr, $data, $phase) = @_;
wrap $_[1], pre => sub { my $self = shift;
die "Must be used as a method" unless ref($self) eq $package;
};
}
}
{
package Test;
sub method_test :Method {
print "This should be called on a reference\n";
}
}
package main;
sub foo : Debug { print "This is the end!\n"; return 123;};
foo(1,2,3);
my $x = bless {}, 'Test';
$x->method_test();
Test::method_test('hello');
__DATA__
$VAR1 = {
'Pre' => [
1,
2,
3
]
};
This is the end!
$VAR1 = {
'Post' => [
1,
2,
3
]
};
This should be called on a reference
Must be used as a method at attributes.pl line 16.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment