package thing; use strict; sub new { my %self; my ($class,@rest) = @_; unless ($#rest%2) { die "Odd parameter count\n"} for (my $k=0; $k<@rest; $k+=2) { my $wotsit = lc($rest[$k]); $wotsit =~ s/-//; $self{$wotsit} = $rest[$k+1]; } bless \%self,$class; } sub AUTOLOAD { my ($where,$val) = @_; our $AUTOLOAD; $AUTOLOAD =~ s/.*://; my ($dirn,$what) = ($AUTOLOAD =~ /(...)(.*)/); if ($dirn eq "get") { return $$where{lc($what)}; } elsif ($dirn eq "set") { $$where{lc($what)} = $val; } return 1; } 1;
Here's a quick example of usage
use thing $house = new thing("type","building","rooms",12,"foundations", "yes"); print $house -> gettype(); $house -> settype("tree"); print " " . $house -> gettype();