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 :)

In reply to OO automatic accessor generation by Neighbour

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.