Phaysis has asked for the wisdom of the Perl Monks concerning the following question:
Hey folks-
I have a set of database classes I'm writing that are customized for each table in my database (allowing for business logic, foreign-key stuff, etc). I've implemented these as tied hashes for the sake of user-code simplicity. Each hash represents one and only one row from the database. When I want to create a new user in the user database table, I do something like this:
use My::DB::User; my (%th,$tho); my %nu = ( # new user data username => 'foobar', password => 'raboof', groupid => 1, url => 'http://www.google.com/', email => 'fake@fakeolameo.como', active => 1, ); # tie it here $tho = tie %th, 'My::DB::User', {-data=>\%nu}; print "username is: $th{'username'}"; #prints "username is: foobar" # save the data to the database $tho->saveData();
This creates a new user and does the SQL INSERT into the proper table. I can load a user from the database in a similar way:
$tho = tie %th, 'My::DB::User', {-userid=>'25'};
This will return a single row (userid of 25) which I can then manipulate as a hash. But what if I wanted to select a set of rows from a table that matched a certain search condition? This would result in a number of rows being returned, not just one, hence my single-row tied hash paradigm breaks down. I'd need an array to handle the tied hashes of rows. It'd be nice if my classes could sniff out the need for a single row or a collection of rows. That I can do.
But what I'm wondering is this: since the variable type in the first position of tie()'s parameters defines which of TIEHASH or TIEARRAY gets called, is it possible, or prudent, to include both of those methods in my parent class? Would this be a good situation where tied typeglobs would work? A typeglob could be sent to the tie() statement and return the correct datatype. Is it possible to tie typeglobs? I don't think so, but I'm not so sure.
Any input would be helpful. Thanks!
(Ph) Phaysis (Shawn)
If idle hands are the tools of the devil, are idol tools the hands of god?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Tied hashes and arrays, yeah. But tied typeglobs?
by bart (Canon) on Sep 29, 2003 at 07:28 UTC | |
|
Re: Tied hashes and arrays, yeah. But tied typeglobs?
by Jasper (Chaplain) on Sep 29, 2003 at 14:03 UTC | |
by Phaysis (Pilgrim) on Sep 29, 2003 at 23:18 UTC | |
|
Re: Tied hashes and arrays, yeah. But tied typeglobs?
by tilly (Archbishop) on Oct 04, 2003 at 15:27 UTC | |
by Phaysis (Pilgrim) on Oct 04, 2003 at 16:49 UTC |