SPUG: Storable and DBI

Creede Lambard creede at penguinsinthenight.com
Sat Oct 26 08:08:50 CDT 2002

Greetings fellow SPUGsters,

I was playing around with the Storable module this morning and ran into
some odd behavior. I wondered if anyone else has ever seen this, and
whether there's a fix or whether I should just code around it.

I have a module that I want to serialize using Storable. This module,
I'll call it Foo, starts out:

    #!/usr/bin/perl -w

    package Foo;

    use DBI;
    use CGI;

The module exports, among other things, an attribute page(), a method
dbh() created using DBI->connect() that gives you a reference to the
database handle generated by DBI, and a method cgi() that gives you a
reference to a CGI object.

So far so good. Everything works OK and I can access my MySQL setup and
write web pages and stuff like that, no problem.

So, I write this program to test out Storable:

    use Storable qw( freeze thaw );
    use Foo;
    use Data::Dumper;
    my $foo = new Foo;
    my $bar = freeze $foo;
    my $baz = thaw $bar;
    print $baz->page;
    print "\n";
    print $baz->cgi->p("I've been stored!");
    print "\n";
    my $query = 'select count(*) as count from main';
    print "$query\n";
    my $sth = $baz->dbh->prepare($query);
    my $row = $sth->fetchrow_hashref;
    print $row->{count};
    print "\n\n";
The first two calls to $baz work fine. I get

    I've been stored!

and the contents of the query print. The database call fails completely.
The clue as to why is contained in this cryptic statement in and among
the output that happens when I thaw()ed $bar:

dbih_getcom handle DBI::db=HASH(0x810636c) is not a DBI handle (has no
magic) at test1.cgi line 17

So, my question is, why is it that the DBI call fails? It can't be just
because it's an object -- if that was it the call to CGI would fail too.

I can code around this by removing the DBI stuff to another module or to
the CGI script that uses those calls, but I'm curious as to why this is
happening, and whether there might be a way around it.


-- Creede

 * .~. `( 
` / V \  . Creede Lambard                : Nothing is quite so powerful
 /(   )\   creede at penguinsinthenight.com :  a penguin whose time has

Perl Programmer and Linux Sysadmin, reasonable rates. Inquire within.
GPG key at http://www.penguinsinthenight.com/creede_public_key.asc
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : http://mail.pm.org/archives/spug-list/attachments/20021026/4399fa9d/attachment.bin

More information about the spug-list mailing list