Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2023-09-24 04:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?