http://www.perlmonks.org?node_id=1002162


in reply to Perl OO: Need a concise way of representing operations on an object

I think you can achieve what you’re looking for by using closures as follows:

{ my ($foo, $bar, $container); my %transformations = ( xform1 => sub { ... }, xform2 => sub { ... }, ); sub transform { (my $self, $container) = @_; for my $xform (@{$self->{xforms}}) { $foo = get_foo(); $bar = get_bar(); $transformations{$xform}->(); } } }

Using this approach, the anonymous subroutines (closures) in %transformations have access to $foo, $bar, and $container without the need for the boilerplate code.

Hope that helps,

Athanasius <°(((><contra mundum