Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Storing Objects in a Database

by chromatic (Archbishop)
on Feb 18, 2000 at 23:44 UTC ( #3711=perlquestion: print w/replies, xml ) Need Help??
chromatic has asked for the wisdom of the Perl Monks concerning the following question:

I'm curious as to the best (or possible) way to store an existing Perl object in a database. I'd use the DBI module.

What I have are a handful of classes (raw objects with default data) in modules -- things are already defined there. I would be creating new instances of those objects with different data. I'd like to store those in a database.

One option would be to gather up all of the data in the instance objects, db-ify it, and use the primary key of that row in the db to pull the data out and put it into a newly made object. That's not a bad solution.

Is there any way I can store new classes and objects in a database without having to use eval all the time or write the objects to the normal filesystem and then require them?

Replies are listed 'Best First'.
Re: Storing Objects in a Database
by btrott (Parson) on Feb 19, 2000 at 01:33 UTC
    Have you looked at Storable? It may help you.

    It can store objects to the filesystem or to memory, then retrieve them, transforming them back into objects.

    So perhaps you could freeze the object to a scalar, store it in a blob in your database, pull it back out, thaw it, then use it again? I don't know--I've only tested Storable using the filesystem, but it seems logical that you could do the same thing w/ a database.

    You might be able to do something like this (untested):

    use Storable qw/freeze thaw/; use DBI; use Foo; # replace this with your module name my $object = new Foo; my $dbh = DBI->connect('bar', 'foo', 'foo', 'Oracle') or die "Can't connect: ", DBI->errstr; # store it my $sth = $dbh->prepare(<<SQL) or die "Can't prepare: ", $dbh->errstr; insert into objects (id, content) values (?, ?) SQL $sth->execute("foo", freeze $object) or die "Can't execute: ", $dbh->errstr; $sth->finish; # now get it back my $retrieve = $dbh->prepare(<<SQL) or die "Can't prepare: ", $dbh->er +rstr; select content from objects where id = ? SQL $retrieve->execute("foo"); my $content = $retrieve->fetchrow; my $unthawed = thaw $content;
    ...and now use $unthawed just as you would your original object.

    Does this sound like what you want?

      Yes, that sounds very good. In my initial perusal of the docpage, I didn't think about using freeze and thaw with DBI. Thank you very much!
Code too?
by Anonymous Monk on Feb 19, 2000 at 22:41 UTC
    Here is a question: I use objects so that each object can implement an interface and have code execute when a method is called. That is why most people use OO, I think. Anyway, how can I serialize those code refs so that I can send an object and its code and its package name across a network, between sessions, et cetra. I mean, this is a big fat security hole, but I still think this ability would be cool. I could write a subroutine that grabs all the instance variables in an object (with something like Storable or Data::Dump) and make sure the package exists on both places I want to use the object and recreate the object with the supplied instance variables. But I don't like that way -- I want to be able to send the whole package namespace and its methods to somewhere that doesn't necessarily have that package. Can it be done? Any ideas? Gary Richardson
      The only idea I've come up with is writing the object to the filesystem in an intermediary step, making sure you have the path in a "use lib" statement, and then requireing it on the remote host. I don't know how well that would work -- but for my purposes, where people can edit objects in real-time, it has some possibilities.
RE: Storing Objects in a Database
by Anonymous Monk on Feb 21, 2000 at 07:33 UTC
    See or (YSMV - Your Spelling May Vary)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://3711]
Approved by root
and the monks are chillaxin'...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2017-10-22 01:07 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (271 votes). Check out past polls.