Welcoming Python::Decorator
November 5, 2008
A few days ago, I wrote about some ideas on how to adapt Python decorators to Perl.
Well, the ideas grew and soon enough I couldn't keep them in my head anymore so it became a module: Python::Decorator, now available on CPAN. This module let's you write Python style decorators in Perl, so that the following Perl code for example would compile and do what you expect:
use Python::Decorator;
# memoize incr's results, print debug info upon
# calling incr and check that the first argument
# is an integer:
@memoize
@debug("incr")
@validate("int")
sub incr {
return $_[0]+1;
}
Of course, Python::Decorator is just a proof of concept. It shows that compile-time function composition (aka macros) can be done in Perl, at least in some restricted way. If such a feature makes it into the Perl core, it will have to have at least a more perlish syntax.
Still, Python::Decorator is fun for at least one more reason: it uses PPI, the Perl parsing module, to analyze and manipulate the source code of Perl programs. Now, that is cool and a lot like macros :)
Well, the ideas grew and soon enough I couldn't keep them in my head anymore so it became a module: Python::Decorator, now available on CPAN. This module let's you write Python style decorators in Perl, so that the following Perl code for example would compile and do what you expect:
use Python::Decorator;
# memoize incr's results, print debug info upon
# calling incr and check that the first argument
# is an integer:
@memoize
@debug("incr")
@validate("int")
sub incr {
return $_[0]+1;
}
Of course, Python::Decorator is just a proof of concept. It shows that compile-time function composition (aka macros) can be done in Perl, at least in some restricted way. If such a feature makes it into the Perl core, it will have to have at least a more perlish syntax.
Still, Python::Decorator is fun for at least one more reason: it uses PPI, the Perl parsing module, to analyze and manipulate the source code of Perl programs. Now, that is cool and a lot like macros :)