Skip to content

Instantly share code, notes, and snippets.

@breezhang
Created April 16, 2014 13:42
Show Gist options
  • Save breezhang/10877394 to your computer and use it in GitHub Desktop.
Save breezhang/10877394 to your computer and use it in GitHub Desktop.
perl attribute demo .......................
use strict;
use warnings;
use Data::Dumper::Concise;
use Scalar::Util qw/reftype/;
use constant DEBUG => 1;
use Attribute::Handlers;
sub _log : ATTR(CODE) {
my ( $pkg, $sym, $code ) = @_;
print reftype( $_[1] );
if (DEBUG) {
my $name = *{$sym}{NAME};
no warnings 'redefine';
*{$sym} = sub {
log_message("Entering sub $pkg\:\:$name");
my @ret = $code->(@_);
log_message("Leaving sub $pkg\:\:$name");
return @ret;
};
}
}
sub log_message {
print shift;
}
sub do_something : _log {
print "I'm doing something.\n";
}
do_something;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment