in reply to How do I get to this 'simple' helper?
eval(' package '.$package.'; sub '.$accessor.' { my $t=shift; $t->{'.$var.'}=$_[0] if @_; $t->{'.$var.'}; } ')
I prefer sprintf.
eval(sprintf( ' package %s; sub %s { my $t=shift; $t->{%s}=$_[0] if @_; $t->{%s}; } ', $package, $accessor, $var, $var ))
Note that I actually build the sub in the right package. That changes some of the meta information associated with the sub (e.g. its __PACKAGE__).
Update: Fixed copy and paste error.
|
|---|