You
could do something like this without any eval magic:
my $o = Anything->new(qw/foo bar baz/);
$o->bar;
package Anything;
use strict;
use warnings;
sub new {
my ($class, @methods) = @_;
my $self = bless {}, $class;
for my $method (@methods) {
no strict 'refs';
*$method = sub { print "this is method $method\n" };
}
return $self;
}
But I'd suggest you don't. Classes like this are usually a sign of a problem that's either not analyzed properly or that doesn't benefit from solving it in an OO way. In any case it doesn't exactly make your program easier to understand or debug.