Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: OO automatic accessor generation

by WizardOfUz (Friar)
on Nov 11, 2009 at 14:17 UTC ( [id://806503]=note: print w/replies, xml ) Need Help??


in reply to OO automatic accessor generation

You shouldn't add the accessor generation code to the constructor (unless you really want to redefine your accessor methods each time you instantiate DataTable).

Try this:

package DataTable;

# Important!
use strict;
use warnings;

my @ATTRIBUTES_SCALAR = qw(
    tablename
);

my @ATTRIBUTES_ARRAY = qw(
    columns
    datatypes
    lengths
    decimals
    signed
    allownull
    default
    usequote
);

my @ATTRIBUTES_HASH = qw(
    indices
);

sub new {
    my $class = shift;
    my $self = bless {}, $class;
    $self->{$_} = undef for @ATTRIBUTES_SCALAR;
    $self->{$_} = []    for @ATTRIBUTES_ARRAY;
    $self->{$_} = {}    for @ATTRIBUTES_HASH;
    return $self;
}

{

    no strict 'refs';

    for ( @ATTRIBUTES_SCALAR ) {
        my $attribute = $_;
        *{ __PACKAGE__ . '::' . $attribute } = sub {
            my $self = shift;
            $self->{$attribute} = shift if @_;
            return $self->{$attribute};
        };
    }

    for ( @ATTRIBUTES_ARRAY ) {
        my $attribute = $_;
        *{ __PACKAGE__ . '::' . $attribute } = sub {
            my $self = shift;
            @{$self->{$attribute}} = @_ if @_;
            return @{$self->{$attribute}};
        };
    }

    for ( @ATTRIBUTES_HASH ) {
        my $attribute = $_;
        *{ __PACKAGE__ . '::' . $attribute } = sub {
            my $self = shift;
            %{$self->{$attribute}} = @_ if @_;
            return %{$self->{$attribute}};
        };
    }

}

1;

Update: Fixed a typo

Replies are listed 'Best First'.
Re^2: OO automatic accessor generation
by Neighbour (Friar) on Nov 12, 2009 at 15:40 UTC

    You're right, after actually getting around to testing it with multiple instances, perl started to complain about sub redefinitions, so the constructor is not the best place to do this :)

    I've now made a base class by shamelessly copying your code :P and made a Datatable class that inherits the base class and adds SQLquery-generating subs.

    Unfortunately, it seems I will need to try both methods of OO-programming...the manual method to further my understanding of things, and using Moose to leave maintainable code for my colleagues :)

    Also, right now the automatically generated accessors don't check anything about the data they are given. This is not how it is supposed to end up (at which point I will probably have to take them out of the autogenerated batch or try something like reassigning a new sub whenever the need arises) but just to get started.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://806503]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-03-19 07:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found