use strict; package Class::InstanceVariables; sub import{ my $class = shift; my $caller = caller(); my $ties = "package $caller;\n".'no strict; $self=""; sub TIESCALAR{ my $class = shift; my $var = shift; bless \$var,$class; } sub FETCH{ my $thing = shift; $self->{$$thing}; } sub STORE{ my $thing = shift; $self->{$$thing} = shift; } '; no strict 'refs'; *{$caller."::self"} = \${$caller."::self"}; foreach my $ivar (@_) { if($ivar!~/^\$([A-Za-z_]\w*)$/) { die "$ivar is not a valid variable name!\n"; } my $varname = $1; *{$caller."\:\:$varname"} = \${$caller."\:\:$varname"}; $ties .= "sub $varname : lvalue { \$_[0]->{$varname} }\n"; $ties .= "tie \${$varname},'$caller','$varname';\n"; } *{$caller."::instance"}=sub{ bless {},$caller; }; eval $ties; if ($@) { die $@; } } 1;