DBD::DBM and hashrefs
am 13.10.2005 19:11:41 von lists
Hello,
I have a question about using DBD::DBM in MLDBM mode: does anyone know
whether it works with hashrefs as values, or just arrayrefs?
Reading the docs and trying the module leads me to believe it only
works with data like:
$Tied_hash{ $id } =3D [ $val1, $val2 ];
But all the data in my DB file looks like:
$Tied_hash{ $id } =3D { key1 =3D> $val1, key2 =3D> $val2 };
.... and I'd like to use DBD::DBM to access it (i.e. "SELECT key1, key2
from foo WHERE id=3D?") but as far as I can tell this is not possible.
Am I right?
Thanks for any help
RT
Re: DBD::DBM and hashrefs
am 14.10.2005 02:19:06 von ron
On Thu, 13 Oct 2005 10:11:41 -0700, Ryan Tate wrote:
Hi Ryan
> $Tied_hash{ $id } =3D { key1 =3D> $val1, key2 =3D> $val2 };
But a hash is just an array of key/value pairs. Can't you just try:
$T{$id} =3D [\%hash];
What happens?
--
Cheers
Ron Savage, ron@savage.net.au on 14/10/2005
http://savage.net.au/index.html
Let the record show: Microsoft is not an Australian company
Re: DBD::DBM and hashrefs
am 14.10.2005 20:41:19 von jeff
> -----Original Message-----
> From: Ryan Tate [mailto:lists@ryantate.com]
> Subject: DBD::DBM and hashrefs
>
> Hello,
>
> I have a question about using DBD::DBM in MLDBM mode: does anyone know
> whether it works with hashrefs as values, or just arrayrefs?
>
> Reading the docs and trying the module leads me to believe it only
> works with data like:
>
> $Tied_hash{ $id } = [ $val1, $val2 ];
>
> But all the data in my DB file looks like:
>
> $Tied_hash{ $id } = { key1 => $val1, key2 => $val2 };
I believe you can specify hash storage by passing the appropriate DBM flags when you do DBI->connect(). Use the flags your DBM implementation expects and pass them as specified in the DBD::DBM docs.
--
Jeff
Re: DBD::DBM and hashrefs
am 15.10.2005 01:12:00 von lists
On Thu, 13 Oct 2005 17:22:07 -0700, Ron Savage wrote:
>Can't you just try:
>
>$T{$id} =3D [\%hash];
The tied hash already exists in the other format and is used by
multiple scripts, so changing the format is not something I want to
do.
The only reason I'm even using DBD::DBM is I want to keep using the
DBM file as I have it, if I were ready to change the whole data
storage format I would just go to a proper relational database
already.
Thanks nevertheless.
RT
Re: DBD::DBM and hashrefs
am 15.10.2005 01:15:55 von lists
On Fri, 14 Oct 2005 11:41:45 -0700, jeff wrote:
>I believe you can specify hash storage by passing the appropriate DBM flag=
s
>when you do DBI->connect(). Use the flags your DBM implementation expects =
and
>pass them as specified in the DBD::DBM docs.
I have been over the docs several times, as well as the source code,
and am knee-deep in writing a patch (on the offhand suggestion of Tim
Bunce on the Class::DBI list).
If DBD::DBM supports hashrefs as values the same way it supports
arrayrefs as values, I am _really_ missing something.
Needless to say, I saw no such flags. If they exist I would love to
see them, so I can chuck this patch.
RT
Re: DBD::DBM and hashrefs
am 15.10.2005 02:05:37 von lists
I've blabbered about this enough, here's some example code I ran just now.
The script:
----------start----------
use strict;
use warnings;
use DBI;
use Data::Dumper;
#Test of DBD::DBM with MLDBM
#MLDBM file format is $Dbm{ $id } =3D {key =3D> $val, key2 =3D> $val2 };
my $dbh =3D DBI->connect('dbi:DBM:mldbm=3DStorable;type=3DDB_File;f_dir= 3D/=
home/sappy/public_html');
$dbh->{RaiseError} =3D 1;
$dbh->{dbm_store_metadata} =3D 0;
#Except for 'id', each column in dbm_cols corresponds
#to keys from the hashrefs in the MLDBM file
$dbh->{dbm_cols} =3D 'id,path,publication_name,regex_headlines';
my $statement =3D $dbh->prepare('SELECT
id,path,publication_name,regex_headlines FROM sites WHERE id=3D?');
$statement->execute('id_services-http-users-r-ryantate-cgi-b in-se_ho_admin-=
23362-1058765796-14\
70540404-3232250416');
my @row =3D $statement->fetchrow_array;
print Dumper(@row);
----------end----------
What I expected as output was an array of four defined, simple scalar
values, corresponding to $id ("id_services ..."), $Dbm{$id}{path}
("/index.html"), $Dbm{$id}{publication_name} ("Dude's Site"), etc.
What I got as output from this test script was $id, a hashref
comprising the entirety of $Dbm{$id}, and two undefined values. More
precisely:
----------start----------
$VAR1 =3D 'id_services-http-users-r-ryantate-cgi-bin-se_ho_admin-23362 -1058=
765796-1470540404-323\
2250416';
$VAR2 =3D {
'match_join_exclude_target' =3D> '',
'match_num_year' =3D> [
'3'
],
'search_target_exact_phrase_atom_handling' =3D> 'Match whole exact phrase=
',
---snip---
'path' =3D> '/index.html',
---snip---
'publication_name' =3D> 'Dude's Site',
---snip---
'regex_headlines' =3D> '
[A-Za-z]+?, ([A-Za-z]+?) (\\d\\d?),
(\\d\\d\\d\\d)
\\s*
ass=3D"blogPost">\\s*(.{1,20})(.*?)\\s*Posted by:
href=3D"([^"]+)">',
'regex_exclude_list' =3D> [],
'next_page_increment_flag' =3D> ''
};
$VAR3 =3D undef;
$VAR4 =3D undef;
----------end----------
Short of patching the module, is there anything I can do to get what I expe=
cted?