http://qs321.pair.com?node_id=1030806


in reply to Re^4: RFC: Simulating Ruby's "yield" and "blocks" in Perl (Python)
in thread RFC: Simulating Ruby's "yield" and "blocks" in Perl

v3 is current, though str.format() was added in late v2 releases for compatibility (printf style interpolation is to be deprecated some time but I'll use it below as an example...). Here's the decorated version as suggested:
Edit: trimmed some code
def rubyyielder(gen): def wrapped_gen(block): for elem in gen(): block(elem) return wrapped_gen @rubyyielder def test(): print("In test") yield 1 print("back in test") yield 2 @test def _(a): print("You are in block %s" % a)
I don't really see a win with this style, though, over giving test a parameter, and just using standard function calls (I had a little play with coroutines on the way but that's really overkill for this).