Skip to content

Instantly share code, notes, and snippets.

@zachwalton
Last active December 10, 2015 12:49
Show Gist options
  • Save zachwalton/4436809 to your computer and use it in GitHub Desktop.
Save zachwalton/4436809 to your computer and use it in GitHub Desktop.
Python Decorators in Perl
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