From: Dave Storrs <[EMAIL PROTECTED]> > I've got a class called APC::Event. The idea is that when you > instantiate one of these objects, you are signalling that a particular > event has happened; ergo, it takes the information you specified, > deduces a bunch more stuff, and logs all of it to a database. > Afterwards it sticks around and provides an interface to that record > (or the whole table, actually). > > I've just discovered the Tie::DBI modules, and they seem like exactly > what I want here, but I'm wondering if the following is going to get > me in trouble: > > > sub new { > my ($proto, %args) = @_; > > my %self; > my $database_handle = $args{ dbh } > || DBH(\%default_database_params, > {PrintError => 0, RaiseError => 1} > ); > tie %self, Tie::DBI, { db => $database_handle, > CLOBBER => 1, # Allow INSERT and > UPDATE, > # but not DELETE > }; > > > my $class = ref $proto # Are we being called as a class or > || $proto # instance method? > || 'APC::Event'; > > bless \%self, $class; > }
I think this should be OK. > I haven't done that much with tie in the past, and I've never > done > anything where I tied an object and then blessed it, so I'm not quite > sure what the implications are. Specifically, > > 1) As I understand it, tie() uses bless() under the hood; is this > correct? Yes, but ... when you tie a variable the TIESCALAR/TIEARRAY/TIEHASH/TIEHANDLE subroutine constructs a data structure, creates a reference to that structure, bless()es the reference, returns the blessed reference and then perl ties the variable in question with that object. So it's not the variable, but the hidden reference that's blessed. > 2) I want the object to be an APC::Event, because there is more in the > class than just a constructor. Assuming that the answer to question 1 > is "yes", what happens when I rebless a reference? I believe it > "forgets" all about its original class, but I've never done it before > (actually, I've tried very hard to avoid it). Yes, if you rebless a reference it forgets about the original class and becomes an object of a different class. But this is not what happens at this time > 3) If I bless %self out of the "Tie::Hash" space and into the > "APC::Event" space, are the "Tie::Hash" functions going to stop > working? (I assume so.) You can't bless a hash, you can only bless a reference. What you end up with after your constructor is something like : APC::Event=HASH(0x1a7f148) reference pointing to a hash tied to a Tie::DBI=HASH(0x486454) reference pointing to whatever data Tie::DBI needs to store. Then $obj->Foo() calls the APC::Event::Foo() subroutine and $val = $obj->{foo} calls Tie::DBI::FETCH() subroutine with the Tie::DBI=HASH(0x486454) reference and 'foo' parameters. Jenda =========== [EMAIL PROTECTED] == http://Jenda.Krynicky.cz ========== There is a reason for living. There must be. I've seen it somewhere. It's just that in the mess on my table ... and in my brain. I can't find it. --- me -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]