package Foobar; sub _setup_xforms { my ($cont, $foo); return (\$cont, \$foo, { xform1 => sub { printf("Level %d,foo=%s\n", $cont, $foo); }, }); }; sub transform { my ($self, $cont) = @_; my ($cont_ref, $foo_ref, $xforms) = _setup_xforms(); $$cont_ref //= $cont; # //= to convince you it's not still set for my $xform (values %$xforms) { $$foo_ref //= 0; # //= as above $xform->(); $$foo_ref++; $self->transform($cont + 1) if ($cont < 5); print "Level $cont done, foo=$$foo_ref\n"; } } sub new { bless({},'Foobar') } my $foo = new Foobar( id => 1 ); $foo->transform(0);