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:
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} = {}; 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;
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} = []; } 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, since this is my first attempt at OO-perl, I humbly submit this to the monks for comments :)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;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: OO automatic accessor generation
by WizardOfUz (Friar) on Nov 11, 2009 at 14:17 UTC | |
by Neighbour (Friar) on Nov 12, 2009 at 15:40 UTC | |
Re: OO automatic accessor generation
by Jenda (Abbot) on Nov 11, 2009 at 15:34 UTC | |
by WizardOfUz (Friar) on Nov 11, 2009 at 16:05 UTC | |
by merlyn (Sage) on Nov 11, 2009 at 16:58 UTC | |
by WizardOfUz (Friar) on Nov 11, 2009 at 18:37 UTC | |
by stvn (Monsignor) on Nov 12, 2009 at 03:56 UTC | |
| |
by chromatic (Archbishop) on Nov 11, 2009 at 20:58 UTC | |
| |
by eric256 (Parson) on Nov 11, 2009 at 21:12 UTC | |
| |
by leocharre (Priest) on Nov 25, 2009 at 19:41 UTC | |
Re: OO automatic accessor generation
by gnosti (Chaplain) on Nov 13, 2009 at 05:43 UTC | |
OT: Random Enthusiasm
by misterwhipple (Monk) on Nov 12, 2009 at 19:12 UTC | |
by misterwhipple (Monk) on Nov 13, 2009 at 19:23 UTC |
Back to
Meditations