On Mon, Feb 21, 2005 at 04:10:29PM -0800, David Wheeler wrote:
> 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'>.

True. The 'if ref' was a little over the top though. If the user
messes with the contents of $h->{Callbacks} then they get what
they've asked for.

Note that the STORE code for "Callbacks" only gets fired
in a few situations. Specifically, doing

        $h->{Callbacks}->{foo} = ...;

will try to FETCH Callbacks first and if it doesn't exist perl will
STORE an *empty* hash ref which is then returned by the $h->{Callbacks}
expression. The following ->{foo} = ... doesn't trigger any code.

> >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).

Yeap. Just me leaving a trail of breadcrumbs to follow later.

> 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?

Yeap. Though the standard way of writing it would be

        if (my $cb = $dbh->{Callbacks}) {
                ...

otherwise $dbh->{Callbacks}{"connect_cached.reused"} will
autovivify the Callbacks hash and so cause a (slight) slowdown in
the method dispatcher for all later uses of the handle.

> >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. :-)

That's great. You've got it to the point were I can't ignore it any
more :-)  I'll apply it and finish it up.

Many thanks David!

So, do you (or anyone else) fancy doing anything else?...

Tim.

Reply via email to