Re: tie/bless interactions?
Peter and Jenda, Thank you both for your information and pointers, I appreciated them. I actually ended up doing something slightly different; I wanted to make method calls use identical syntax, whether they were going to the APC::Event or the Tie::DBI, so what I ended up doing was giving the APC::Event a has-a relationship with the Tie::DBI, and then delegating (through AUTOLOAD) calls to the Tie::DBI. It ended up working great! Dave Storrs -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
tie/bless interactions?
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 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? 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). 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.) 4) If the answer to 3 is yes, can I solve this by doing this: my $rh_self = \%self; unshift @{$rh_self-ISA}, 'APC::Event'; 5) Given the above constructor code, am I likely to have any problems with circular references? Thanks in advance, Dave Storrs PS If you wanted to rewrite the code snippet in question 4 without taking a reference, how would you do it? This doesn't seem right: unshift @self{ISA}, 'APC::Event'; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: tie/bless interactions?
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]
Re: tie/bless interactions?
At 02:19 AM 3/14/02 +0100, Jenda Krynicky wrote: 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. Damian Conway's Regexp::Common module does more or less exactly this. I just finished reading the Perl Conference proceedings on it (yes, in places I am that far behind) and hope to make a full recovery :-) -- Peter Scott Pacific Systems Design Technologies http://www.perldebugged.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]