Hashes of Arrays and databases

Joe Oppegaard joe at joppegaard.com
Sun Dec 23 00:25:00 CST 2001


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? I
haven't seen any documentation that says it isn't, although I also have
been able to find any code that actually does it either...

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. (And of course, not everyone has root access to a MySQL
or Oracle database).


#!/usr/bin/perl -w
### This first program creates the database

use GDBM_File;
use strict;

tie my %places, 'GDBM_File', 'places.gdbm', &GDBM_WRCREAT, 0640;

%places = (
        washington      => [ "vancouver", "tacoma", "seattle" ],
        oregon          => [ "portland", "eugene", "beaverton" ],
        arizona         => [ "tempe", "flagstaff", "sedona" ],

untie %places;


#!/usr/bin/perl -w
# This one then tries to read the data, but fails returning
# Can't use string ("ARRAY(0x80f6460)") as an ARRAY ref...
# Without strict enabled it simply prints ARRAY(0x80f6460)

use strict;
use GDBM_File;

tie my %places, 'GDBM_File', 'places.gdbm', &GDBM_READER, 0640;

print $places{washington}[1] . "\n";

untie %places;

--------End Code--------

Ok, now the following code is exactly the same except instead of using
arrays (anonymous or named) as the values, I just use a single value.

Note the following works exactly as expected, printing out vancouver.


#!/usr/bin/perl -w
use strict;
use GDBM_File;

tie my %places, 'GDBM_File', 'single_places.gdbm', &GDBM_WRCREAT, 0640;

%places = (
        washington      => "vancouver",
        oregon          => "portland",
        arizona         => "tempe",

untie %places;


#!/usr/bin/perl -w
use strict;
use GDBM_File;

tie my %places, 'GDBM_File', 'single_places.gdbm', &GDBM_READER, 0640;

print $places{washington} . "\n";

untie %places;

--------End Code--------

Any ideas? The other option I've been considering is just inputting the
data into a normal text file, but encrypting the information (Maybe using
Crypt::Blowfish??) before it got there, because there will be passwords
stored on my end project.

If you care what I'm shooting for, read the following, if not, skip this
next paragraph of rambling. :)

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) :) I have a pretty good idea on everything else
I need to do, this is just my main barrier.

Have a good one,

-Joe Oppegaard


More information about the Pdx-pm-list mailing list