March 2014 Archives

Python's decorators in Perl

Python's decorator is some syntax sugar for wrapping functions by another functions:

def bold(func):
    def wrapper(str):
        return '<b>' + func(str) + '</b>'
    return wrapper

def div_id(id):
def decorator(func):
def wrapper(str):
return (('<div id="%s">' % id) + func(str) + '</div>')
return wrapper
return decorator

@div_id("testid")
@bold
def p(str):
return '<p>' + str + '</p>'

print p("test")

About Piotr Roszatycki

user-pic I blog about Perl.