# class Foo # attr_reader :val # def initialize(val) # @val = val # end # def inc(some_val) # @val = @val + some_val # end # end # def testy(args) # foo = Foo.new(1) # _testy(args, foo) { # yield foo # } # end # def _testy(args,somefoo) # for item in args # somefoo.inc(item) # yield # end # end # testy([1,2,3]) {|x| puts x.val} use strict; use Coro::Generator; { package Foo; sub new { bless { val => $_[1] } => $_[0] } sub inc { $_[0]->{val} += $_[1] } sub val { shift->{val} } } my $inner_testy = generator { my $foo = pop; foreach my $item (@_) { $foo->inc($item); yield($foo); } yield undef; }; sub testy { my $action = pop; my $foo = Foo->new(1); $action->($foo) while $inner_testy->( @_, $foo ); } testy(1,2,3, sub { print $_[0]->val . "\n" });