What is wrong with accessing DBI directly?

Posted by canavanin on Stack Overflow See other posts from Stack Overflow or by canavanin
Published on 2011-01-12T11:10:02Z Indexed on 2011/01/12 11:53 UTC
Read the original article Hit count: 276

Filed under:
|
|
|

Hi everyone!

I'm currently reading Effective Perl Programming (2nd edition). I have come across a piece of code which was described as being poorly written, but I don't yet understand what's so bad about it, or how it should be improved. It would be great if someone could explain the matter to me.

Here's the code in question:

sub sum_values_per_key {
   my ( $class, $dsn, $user, $password, $parameters ) = @_;
   my %results;

   my $dbh = 
     DBI->connect( $dsn, $user, $password, $parameters );

   my $sth = $dbh->prepare(
     'select key, calculate(value) from my_table');
   $sth->execute();

   # ... fill %results ...

   $sth->finish();
   $dbh->disconnect();

   return \%results;
}

The example comes from the chapter on testing your code (p. 324/325). The sentence that has left me wondering about how to improve the code is the following:

Since the code was poorly written and accesses DBI directly, you'll have to create a fake DBI object to stand in for the real thing.

I have probably not understood a lot of what the book has so far been trying to teach me, or I have skipped the section relevant for understanding what's bad practice about the above code... Well, thanks in advance for your help!

© Stack Overflow or respective owner

Related posts about perl

Related posts about unit-testing