Search code examples
perldbixs

How can I subclass DBI to hide user credentials?


I want to subclass the DBI module to hide the connect subroutine with C code. For example, I have:

$dbh = DBI->connect($data_source, $username, $auth, \%attr);

I want to write some C code which calls the above DBI->connect subroutine and returns the $dbh handle in Perl.

Is that possible to do and if so, could someone provide an example or point to some sources?


Solution

  • Why subclass? Just create a sub!

    sub my_connect {
       # Get from config file or whatever
       my $user   = ...;
       my $passwd = ...;
    
       return DBI->connect($data_source, $username, $auth, \%attr);
    }