in reply to Perl OOP variables

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.

Replies are listed 'Best First'.
Re: Re: Perl OOP variables
by BMaximus (Chaplain) on May 31, 2001 at 09:53 UTC
    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