Last active
December 10, 2015 12:49
-
-
Save zachwalton/4436809 to your computer and use it in GitHub Desktop.
Python Decorators in Perl
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
Modified from http://lemonnier.se/erwan/blog/item/45/ | |
# | |
# connTest is our decorator subroutine. | |
# | |
sub connTest { | |
my $f = @_; | |
return sub { | |
&$f(); | |
} | |
} | |
# | |
# This is our example subroutine that we're going to wrap in the @connTest decorator. | |
# | |
sub example { | |
print @_; | |
} | |
# | |
# We append this to the bottom of the declaration of each subroutine that we want to wrap | |
# in a decorator. It works by updating the symbol table with a reference to the decorator, | |
# which then calls the original function and returns. | |
# | |
$::{'example'} = \&connTest(\&example); | |
# Call our function! | |
$> example("1","2") | |
12 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment