Hashes of Arrays and databases

Curtis Poe poec at yahoo.com
Sun Dec 23 04:59:23 CST 2001

--- Joe Oppegaard <joe at joppegaard.com> wrote:
> Heya,
> I'm trying to add hashes of arrays to a database, but accessing those
> values again is the problem. First of all, is this even possible? 


This isn't terribly difficult.  There are a number of strategies that you can employ to pull this
off.  I think the easiest is to use Storable (you can grab it off of the CPAN).  This snippet
shows you how you can "serialize" the data structure to a scalar and read it back out.  The scalar
can be saved to a database, written out over a socket connection or whatever.  Run this snippet
and you'll see what I mean.

    use strict;
    use warnings;
    use Storable qw/ freeze thaw /;
    use Data::Dumper;
    my %foo = (
        one => [ qw/ a b c / ],
        two => [ qw/ 1 2 3 / ]
    my $serialized = freeze \%foo;
    my %clone = %{ thaw $serialized };
    print Dumper \%clone;

> In the following code I'm using the GDBM and anonymous arrays, although
> when trying to figure this out I've used named arrays and anonymous arrays
> both in NDBM, BSD-DB and GDBM. I'd like to stay away from databases like
> MySQL and Oracle because when this project is finished I'd like it to be
> easily portable.

If you want it to be easily portable, then use a database and the DBI module :)
> In the end what I'm hoping to accomplish is a fully featured webpage
> basically cloning http://conversatron.com, mainly because that site is
> hilarous and I'd to see other people start off their own Q&A site. There
> already is a couple of sites like this (http://truemeaningoflife.com and
> http://conversawang.com), but no one has released their source! The
> conversatron uses python and mysql, both the wang and tmol use php (I
> don't know what database), and I'd like to see it done in perl (with an
> easily portable db) :)

If you want an easily portable solution, stick to DBI as far more people will be familiar with it.
 Of course, you will have database considerations.  Do you want to support transactions?  Row
level locking?  Foreign key constraints?  If so, databases like MySQL just won't cut it.  If you
ignore those for greater portability and stick to ANSI SQL, this will be *very* portable, but then
much of the work that should be in the database (such as maintaining referential integrity) is
pushed into the code and this will increase the number of bugs in your code (note that I did NOT
say "the probability of the number of bugs".  You will have more bugs if your Perl is forced to
implement those things that your database cannot).

Curtis "Ovid" Poe

"Ovid" on http://www.perlmonks.org/
Someone asked me how to count to 10 in Perl:
push at A,$_ for reverse q.e...q.n.;for(@A){$_=unpack(q|c|,$_);@a=split//;
shift at a;shift at a if $a[$[]eq$[;$_=join q||, at a};print $_,$/for reverse @A

Do You Yahoo!?
Send your FREE holiday greetings online!

More information about the Pdx-pm-list mailing list