use strict; package Class::InstanceVariables; sub import{ my $class = shift; my $caller = caller(); my $ties = <<"EOH" # line 1 "@{[__PACKAGE__]} eval @ line @{[__LINE__]}" package $caller; no strict; EOH . <<'EOS'; $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; } EOS 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;