package Autoattr; our $DEBUG; sub new($) { $DEBUG and printf STDERR "%s::new(%s)\n", __PACKAGE__, join ', ', map "'$_'", @_; ref $_[0] ? bless {%{$_[0]}}, ref $_[0] : bless {}, $_[0]; } sub DESTROY { $DEBUG and printf STDERR "%s::DESTROY(%s)\n", __PACKAGE__, join ', ', map "'$_'", @_; return; } sub AUTOLOAD { no strict qw(refs); $DEBUG and printf STDERR "%s::AUTOLOAD=%s(%s)\n", __PACKAGE__, our $AUTOLOAD, join ', ', map "'$_'", @_; *{$AUTOLOAD} = # into the symbol table $AUTOLOAD =~ /(?{$attr} : ${"$_[0]::$attr"}}; } : $AUTOLOAD =~ /(?{$attr} : ${"$_[0]::$attr"} = $_[1]; $_[0]; }; } : do { # get/set depending on @_ (my $get = $AUTOLOAD) =~ s/([^:]*)$/get_$1/; (my $set = $AUTOLOAD) =~ s/([^:]*)$/set_$1/; sub($;$) {$#_ ? $_[0]->$set(@_[1 .. $#_]) : $_[0]->$get}; } ; $AUTOLOAD =~ /([^:]*)$/; goto $_[0]->can($1); # go to the new symbol }