Just a Theory

By David E. Wheeler

Posts about DBI::Shell

Hack: Force DBI::Shell to use a DBI Subclass

So I just had a need to use DBI::Shell with a subclass of the DBI. It doesn’t support subclasses directly (it’d be nice to be able to specify one on the command-line or something), but I was able to hack it into using one anyway by doing this:

use My::DBI;
BEGIN {
    sub DBI::Shell::Base::DBI () { 'My::DBI' };
}
use DBI::Shell;

Yes, it’s extremely sneaky. DBI::Shell::Base uses the string constant DBI, as in DBI->connect(...), so by shoving a constant into DBI::Shell::Base before loading DBI::Shell, I convince it to use my subclass, instead.

Looking for the comments? Try the old layout.

More about…