package Foo;
=head1 SYNOPSIS
my $f = Foo->new(
bar => 'rab',
qux => 'xuq',
);
=cut
use constant ACCESSOR_MAP => {
'get_bar' => 'bar',
'get_qux' => 'qux',
};
use constant ACCESSOR_ITEM => qw(ping ting zing);
sub new {
my ( $c, %args ) = @_;
my $class = ref $c || $c;
bless {%args}, $class;
}
sub __mk_accessor {
my ( $c, $name, $key ) = @_;
$key ||= $name;
my $pkg = ref $c || $c;
my $sym = join( '::', $pkg, $name );
no strict 'refs';
*{$sym} = sub {
my ($self) = @_;
$self->{$key};
};
}
# set up ping(), ting(), zing()
__PACKAGE__->__mk_accessor($_) foreach __PACKAGE__->ACCESSOR_ITEM;
# set up get_bar(), get_qux()
for my $k ( keys %{ __PACKAGE__->ACCESSOR_MAP } ) {
my $v = __PACKAGE__->ACCESSOR_MAP->{$k};
__PACKAGE__->__mk_accessor( $k, $v );
}
# not tested