in reply to Hash in class?

Another way, with auto-generated accessors, and parameter name validation. Also allows setting parameters in the "new" call. Avoids namespace collision by using a separate namespace for USERSETTINGS.
use strict; use warnings; {package MyClass; my @usersettings = qw|bgcolor fgcolor carmodel hairstyle sexualprefere +nce foo bar baz|; foreach my $s (@usersettings) { eval "sub setting_$s : lvalue { \$_[0]->{USERSETTINGS}{$s} }" +; $@ and die "Error setting member $s : $@" } sub new { my $class = shift; my %options = @_; # Remaining params are optional user settings #my $self = { USERSETTINGS=> {map # {exists $options{$_} ? ($_=>$options{$_}):()} # @usersettings }}; my $self = {}; for my $opt(keys %options){ grep {$opt eq $_} @usersettings or die "invalid option:$opt"; eval "setting_$opt(\$self) ='$options{$opt}'" ; $@ and die "Error setting option $opt : $@" } return bless $self,$class; } 1; } #--------------------------------------- package main; my $obj = MyClass->new( carmodel=>"BMW" ); $obj->setting_foo = "bar"; print "FOO=" . $obj->setting_foo . "; car=" . $obj->setting_carmodel ."; \n";

     Syntactic sugar causes cancer of the semicolon.        --Alan Perlis