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?


In reply to Tied hashes and arrays, yeah. But tied typeglobs? by Phaysis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.