use warnings; use strict; package Coroutine; sub prototype { my $class = shift; bless shift(), $class; } sub instance { my $self = shift; my @sections = $self->(@_); return sub { return () unless @sections; (shift @sections)->(@_); } } package main; my $coroutine = Coroutine->prototype( sub { my $foo = shift; my @bar = @_; ( sub { print "1st section:\n"; print " $_\n" for @bar; return $foo++; }, sub { print "2nd section:\n"; print " $foo\n"; return rand() > .5 ? 'weird' : ++$foo; }, sub { print "3rd section:\n"; print " The end is near - goodbye cruel "; return pop @bar; } ) } ); my $wacky = $coroutine->instance(42, 'hello', 'world'); print "returned($_)\n" while ($_ = $wacky->()); print "Resetting...\n"; $wacky = $coroutine->instance('another', 'time', 'through'); print "returned($_)\n" while ($_ = $wacky->()); __END__ 1st section: hello world returned(42) 2nd section: 43 returned(44) 3rd section: The end is near - goodbye cruel returned(world) Resetting... 1st section: time through returned(another) 2nd section: anothes returned(anothet) 3rd section: The end is near - goodbye cruel returned(through)