On Feb 21, 2005, at 2:17 PM, Tim Bunce wrote:

Which is also pretty easily done, eh? Something like this?

Yes, though I wouldn't bother validating the contents of the hash. Just checking you're getting a hash ref is enough I think.

Well, I figured it was faster to do it up-front (during the construction of a database handle) than for each and every call to a method (where you had C<if ref $cb eq 'CODE'>.

Please forgive my newbie XS coding. And yes, I know this doesn't compile. I
couldn't figure out where to declare Callbacks.

Pick another similar attribute, say CachedKids, and grep to see where/how
that's setup and used. Shout if you want more clues ;)

I did do that, but didn't see it.

/me looks again...

Oh, different file, DBIXS.h. Duh. Got it, now.

- # if the caller has provided a callback then call it
- # especially useful with connect_cached() XXX not enabled/tested/documented
- if (0 and $dbh and my $oc = $dbh->{OnConnect}) { # XXX
- $oc->($dbh, $dsn, $user, $pass, $attr) if ref $oc eq 'CODE';
+ # If the caller has provided a callback then call it
+ if ($dbh and my $cb = $dbh->{Callbacks}{connect}) {
+ $cb->($dbh, $dsn, $user, $pass, $attr);
}

The dispatcher (written in C) will handle the normal pre- and post-method callbacks,
so the explicit post-connect callback above wouldn't be needed in perl code.

Oh. Well the Perl stuff is what I know, and I was going for a working implementation based on my knowledge, as a proof-of-concept. In fact, Perl code is where you had it, too (commented out, for all practical purposes). Feel free to re-implement for performance to your heart's content.

If any method wants to have extra callbacks then it could define them to have
a "method.foo" name like connect_cached.reused here:

+ # If the caller has provided a callback then call it
+ if (my $cb = $dbh->{Callbacks}{"connect_cached.reused"}) {
+ $cb->($dbh, $dsn, $user, $auth, $attr);
+ }

Right. Would these be arbitrary, then, defined on a method-by-method basis?

Umm, it would be handy for the post-method callback to be able to access
the return value of the method (or at least the first value if it's a list).
I think temporarily aliasing $_ would cover that nicely.

Gotcha.

p.s. Don't forget tests, and then there's PurePerl support to consider...
Feel free to throw it back at me if it looks like too much work, or just
checkin bite sized pieces and see how it goes. Probably makes sense for
me to do at least the dispatcher changes.

I was going to add a couple of tests once I got it compiling. It's compiling now, but I think I've bitten off more than I can chew in getting to work. The C is getting over my head. :-(. I've done some stuff, blindly, and attached the patch. At least the Pure Perl version is working. :-)

Regards,

David


Attachment: callbacks.patch
Description: Binary data

Reply via email to