{ package user; { #start lexical scope my @class_attributes = qw/ name password /; no strict 'refs'; for my $attribute ( @class_attributes ) { # Create new Package with Overload eval 'package '.__PACKAGE__.'::'.$attribute.';'.'use overload \'""\' => \&get;'; # Create method new for new package *{__PACKAGE__.'::'.$attribute.'::'.'new'} = sub { die "Not enough Paramters" if @_ < 2; bless \$_[1], $_[0]; }; # Create method set/get for new package *{__PACKAGE__.'::'.$attribute.'::'.'get'} = sub { return ${$_[0]} }; *{__PACKAGE__.'::'.$attribute.'::'.'set'} = sub { ${$_[0]} = $_[1] }; # Create Methods in this package to Call the Classes *{$attribute} = sub { my $self = shift; if (@_) { $self->{$attribute}->set(@_) } else { $self->{$attribute} } }; } } #end lexical scope sub new { my $self = bless {}, shift; my %options = @_; $self->{name} = user::name->new ( $options{name} ); $self->{password} = user::password->new( $options{password} ); return $self; } } my $paul = user->new; $paul->name("Paulchen"); $paul->password("123456"); my $anne = new user; $anne->name("anne"); $anne->password("654321"); print $paul->name, "\n", $paul->password, "\n"; print "\n"; print $anne->name, "\n", $anne->password, "\n";