http://qs1969.pair.com?node_id=771388

Ovid has asked for the wisdom of the Perl Monks concerning the following question:

I have some code which looks like this:

foreach my $block ($self->get_blocks) { my %data = map { $_ => $block->$_ } qw/ package code setup teardown /; eval <<" END"; package $data{package}; \$data{setup}->(); \$data{code}->(); \$data{teardown}->(); END $self->_croak($@) if $@; }

Frankly, I don't like the eval there because it obfuscates the code and breaks syntax highlighting. What I really want is something conceptually similar to this:

foreach my $block ($self->get_blocks) { package $block->package; eval { $block->setup->(); $block->code->(); $block->teardown->(); }; $self->_croak($@) if $@; }

That's much easier to read and it's regrettably a problem I face all of the time. Is there an easy way to force code to run in a different package when I don't know the package until runtime?