# Foo.pm package Foo; use Exporter 'import'; our @EXPORT_OK = qw(method1_foo method2_foo); sub new { return Foo::Object->new( method1 => sub { shift; method1_foo(@_) }, method2 => sub { shift; method2_foo(@_) }, ); } sub method1_foo { # do method1 } sub method2_foo { # do method2 } package Foo::Object; sub new { my ( $class, %method ) = @_; # adds methods while ( my ( $method, $code_ref ) = each %method ) { next if ref $code_ref ne 'CODE'; my $slot = __PACKAGE__ . "::$method"; { no strict 'refs'; *$slot = $code_ref; } } return bless \do { my $anon_scalar }, $class; } 1;