http://qs321.pair.com?node_id=806486

So I've been reading up on perltoot and perltooc and have been trying to create my very first perl class.. It started like this:
package DataTable; sub new { my $class = shift; my $self = {}; $self->{tablename} = undef; $self->{columns} = []; $self->{indices} = {}; bless($self, $class); return $self; } sub tablename { my $self = shift; if (@_) { $self->{tablename} = shift; } return $self->{tablename}; } sub columns { my $self = shift; if (@_) { @{$self->{columns}} = @_; } return @{$self->{columns}}; } sub indices { my $self = shift; if (@_) { %{$self->{indices}} = @_; } return %{$self->{indices}}; } 1;
But then, after having to add more array properties (datatypes, lengths, decimals, allownull, default) and copypasting the accessor subs, something itched. So I revised it to..
package DataTable; sub new { my $class = shift; my $self = {}; $self->{tablename} = undef; $self->{columns} = []; $self->{indices} = {}; $self->{datatypes} = []; $self->{lengths} = []; $self->{decimals} = []; $self->{signed} = []; $self->{allownull} = []; $self->{default} = []; $self->{usequote} = []; } bless($self, $class); return $self; } # Accessor methods sub ArrayAccessor { my $arrayname = shift; my $self = shift; if (@_) { @{$self->{$arrayname}} = @_; } return @{$self->{$arrayname}}; } sub HashAccessor { my $hashname = shift; my $self = shift; if (@_) { %{$self->{$hashname}} = @_; } return %{$self->{$hashname}}; } sub ScalarAccessor { my $scalarname = shift; my $self = shift; if (@_) { $self->{$scalarname} = shift; } return $self->{$scalarname}; } sub tablename { return ScalarAccessor("tablename", @_); } sub columns { return ArrayAccessor("columns", @_); } sub indices { return HashAccessor("indices", @_); } sub datatypes { return ArrayAccessor("datatypes", @_); } sub lengths { return ArrayAccessor("lengths", @_); } sub decimals { return ArrayAccessor("decimals", @_); } sub signed { return ArrayAccessor("signed", @_); } sub allownull { return ArrayAccessor("allownull", @_); } sub default { return ArrayAccessor("default", @_); } sub usequote { return ArrayAccessor("usequote", @_); } 1;
However, seeing the long list of accessor function calls, I still found the need for even more abstraction:
package DataTable; sub new { my $class = shift; my $self = {}; $self->{tablename} = undef; $self->{columns} = []; $self->{indices} = {}; $self->{datatypes} = []; $self->{lengths} = []; $self->{decimals} = []; $self->{signed} = []; $self->{allownull} = []; $self->{default} = []; $self->{usequote} = []; # Automatically create Accessor methods foreach (keys(%{$self})) { my ($type, $prefix, $suffix) = (ref($self->{$_}), "", ""); if ($type eq "ARRAY") { $prefix = '@{'; $suffix = '}'; } if ($type eq "HASH") { $prefix = '%{'; $suffix = '}'; } eval("sub $_ { " . 'my $self = shift; if (@_) { ' . $prefix . '$self->{' . $_ . '}' . $suffix . +' = @_; } return ' . $prefix . '$self->{' . $_ . '}' . $suffix . '; }'); } bless($self, $class); return $self; } 1;
However, since this is my first attempt at OO-perl, I humbly submit this to the monks for comments :)