%object okey1 ikey1 = value ikey2 = value okey2 ikey3 = value ikey4 = value #### { # bare block to limit the scope of %obj_structure # use closures to create accessor methods for each attribute my %obj_structure = ( okey1 => [ qw( ikey1 ikey2 ) ], okey2 => [ qw( ikey3 ikey4 ) ] ); foreach my $okey ( keys %obj_structure ) { foreach my $ikey ( @{ $obj_structure{$okey} } ) { # use symbolic refs to access the symbol table no strict 'refs'; *$var = sub { my $self = shift @_; my ( $value ) = shift @_; if( defined $value ) { $self->{$okey}{$ikey} = $value; } return $self->{$okey}{$ikey}; } } } }