Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^3: Why is a hash the default "object" in oo perl?

by blokhead (Monsignor)
on Jul 18, 2004 at 17:02 UTC ( #375401=note: print w/replies, xml ) Need Help??


in reply to Re^2: Why is a hash the default "object" in oo perl?
in thread Why is a hash the default "object" in oo perl?

See Class::Tables for the full code. The basic idea is that the object implementation is a blessed scalar holding the tuple's primary key. I can also tell what database table it belongs to by what class it's blessed into. I use these two pieces of information to address a deep hash with all the accessor data. The general idea is:
my $DATA; sub id { ${+shift} } # $obj->column_name() # $obj->field("column_name") sub field { my ($self, $col) = @_; my $table = (ref $self)->_table; my $id = $self->id; return $DATA{$table}{$id}{$col}; }
The DESTROY method I gave in the previous post was for the benefit of theAcolyte and the basic inside-out mechanism. You're right that in my code, since the data should stay around as long as there's an object that might need it, I have to do something a little more clever. I end up doing my own form of reference counting. Each time I make an object, I increment $COUNT{$table}{$id}. Each time an object is destroyed, I decrement that count and if the count became zero, clear out the appropriate part of the %DATA hash (and %COUNT as well).

blokhead

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://375401]
help
Chatterbox?
[ambrus]: As for TeX, I'm not trying to discourage anyone from writing carefully beautifully typeset documents, in maths or outside. But most people aren't willing to do that, and will spend only little time about the formatting,
[ambrus]: and try to leave everything else to automated systems without checking how what they write came out format-wise, and for those people, discounting the part about journals with a specific format above,
[ambrus]: just blindly recommending to use LaTeX is a bad idea now.
[Discipulus]: I havery limited needs in such field. Sometimes (for children party invitations) i draw something by hand and I scan it to include into an ms doc.
Discipulus havery can be a good neologism for: have very..;=)

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2017-09-26 11:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (293 votes). Check out past polls.

    Notices?