Created
April 16, 2014 13:42
-
-
Save breezhang/10877394 to your computer and use it in GitHub Desktop.
perl attribute demo .......................
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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