BMaximus has asked for the wisdom of the Perl Monks concerning the following question:

I'm cleaning up one of my modules. Its use requires that you make a separate package out of it so as to attach a name to it that is the same name as a table in Oracle or MySQL.
# to use it you need to create a package named Tables package Tables; use strict qw(var subs); use vars qw(@ISA $table %dates %enums $primarykey); # after this you put the tables here like so package <TABLE NAME IN ALL CAPS>; @ISA = qw(BaseData); $table = "<TABLE NAME IN ALL CAPS>"; $primarykey = "<PRIMARY KEY>"; %dates = (); # list fields with dates here %enums = (); # list fields of type enum here
As you can see. There isn't much to the derived package. All the methods are inherited. However the variables $table $primarykey %dates and %enums are all defined and initialized here and need to be available to BaseData. Here's how its done now.
# I'll use the insert sub as an example sub insert { my $class = shift; my ($table,$seq_query,$primary_key); # grab the variable defined from the derived class # this is hacky $table = ${$class . "::table"}; $primary = ${$class . "::primarykey"};
Any better way of doing the above formentioned so as to let strict do its job in totality or have it not be so hackish?

Thank you,
BMaximus

Replies are listed 'Best First'.
Re: Perl OOP variables
by chromatic (Archbishop) on May 31, 2001 at 09:42 UTC
    Why not write accessor methods? You can write accessors to instance data (stuff stored in a blessed hash, for example, or a blessed array, or a blessed pseudo hash...) or to class data.
    sub table { my $class = shift; $table = shift if @_; return $table; } sub primary { my $class = shift; $primary = shift if @_; return $primary; }
    In just a couple of lines, you have get/set accessors. Modify as necessary, but that's an easier way to handle it.
      I need to able to access the methods like this though.
      There is a table in the database named MEMBERS. So we insert something in to the table like so.
      my $member_id = MEMBERS->insert(\%ins_hash);
      How would I write the module without the need for a constructor?

      BMaximus
        Use Tie::DBI. Example:
        use Tie::DBI; tie %db,Tie::DBI,{db => "mysql:Perlmonks", table => "Reputation", key => "nodeid", user => $def_dbuser, password => $def_dbpw, CLOBBER => 3};
        You can call it %Reputation if you want.

        Then, you can access the table directly and intuitively.

        # # Find all the new, deleted and changed entries # And find the longest title # calculate min and max reputations and total # Just want to make one pass through all of the list # for (keys %db) { my $type = $db{$_}->{type}; next if $type eq 'X'; push (@newnodes, $_) if $type eq 'N'; push (@deletednodes, $_) if $type eq 'D'; push (@changednodes, $_) if $type eq 'C'; $longest_title = max ($longest_title, length ($db{$_}->{title})) +; $total += $db{$_}->{reputation}; $repmax = max ($repmax, $db{$_}->{reputation}); $repmin = min ($repmin, $db{$_}->{reputation}); }
        This sample from my rewrite of luke_repwalker.pl, which I have tweaked to use Tie::DBI.
        --
        Brovnik