title: Feedback, question about my module and if i should change anything?
body: <pre><code>package My::Module;
  
  # $Id$
  
  use strict;
  use Carp;
  use Data::Dumper;
  use DBI;
  
  $My::Module::VERSION  = '0.1';
  
  sub new {
      my ($class, %opt) = @_;
      my $opt_count = keys %opt;
  
      $class-&gt;set_error('');
      #return $class-&gt;set_error("Too many arguments to initialize.") if ($opt_count &gt; 5);
      #return $class-&gt;set_error("Missing arguments to initialize.") if ($opt_count &lt; 2);
  
      my $self = bless {
                        _DRIVER_OPTIONS  =&gt; $opt{'mysql'},
                       },$class;
  
      if (not defined $self) {
          return $class-&gt;set_error( "new() failed: " . $class-&gt;errstr );
      }
  
      if ($self-&gt;{_DRIVER_OPTIONS}-&gt;{Host} ne '') {
          $self-&gt;{_DRIVER_OPTIONS}-&gt;{DataSource} = 'DBI:mysql:database=' . $self-&gt;{_DRIVER_OPTIONS}-&gt;{Database} . ';host=' . $self-&gt;{_DRIVER_OPTIONS}-&gt;{Host};
      } else {
          $self-&gt;{_DRIVER_OPTIONS}-&gt;{DataSource} = 'DBI:mysql:database=' . $self-&gt;{_DRIVER_OPTIONS}-&gt;{Database} . ';';
      }
      $self-&gt;{Handle} = DBI-&gt;connect($self-&gt;{_DRIVER_OPTIONS}-&gt;{DataSource},
                                     $self-&gt;{_DRIVER_OPTIONS}-&gt;{Username},
                                     $self-&gt;{_DRIVER_OPTIONS}-&gt;{Password},
                                     { RaiseError=&gt;1, PrintError=&gt;1, AutoCommit=&gt;1 }
                                    );
      return $self-&gt;set_error("new(): couldn't connect to database: " . DBI-&gt;errstr) unless ($self-&gt;{Handle});
      $self-&gt;{_disconnect} = 1;
  
      print Dumper \$self;
  
      return $self;
  }
  
  sub database {
      my $self = shift;
      if (@_) { $self-&gt;{Handle} = shift }
      return $self-&gt;{Handle};
  }
  
  sub set_error {
      my $class   = shift;
      my $message = shift;
      $class = ref($class) || $class;
      no strict 'refs';
      ${ "$class\::errstr" } = sprintf($message || "", @_);
      return;
  }
  
  *error = \&amp;errstr;
  sub errstr {
      my $class = shift;
      $class = ref( $class ) || $class;
  
      no strict 'refs';
      return ${ "$class\::errstr" } || '';
  }
  
  sub DESTROY {
      my $self = shift;
  
      unless (defined $self-&gt;{Handle} &amp;&amp; $self-&gt;{Handle}-&gt;ping) {
          $self-&gt;set_error(__PACKAGE__ . '::DESTROY(). Database handle has gone away');
          return;
      }
  
      unless ($self-&gt;{Handle}-&gt;{AutoCommit}) {
          $self-&gt;{Handle}-&gt;commit;
      }
  
      if ($self-&gt;{_disconnect}) {
          $self-&gt;{Handle}-&gt;disconnect;
      }
  }
  
  1;
  </code></pre>
  
  <ol>
  <li><p>Is that the right way so i can
  re-use the database on my code
  instead of having to open a new
  connection or that will aswell open
  a new connection every time i use it
  ?</p></li>
  <li><p>Should i change anything on the
  module ? or anything i did wrong ?</p></li>
  </ol>
  
  <p>Currently i am just learning and thinked of doing my own engine module so i began with this.</p>
  
  <p>Simple test code (the bellow code is not to be reviewed just a sample on how to use the module):</p>
  
  <pre><code>#!/usr/bin/perl
  
  use warnings;
  use strict;
  use Data::Dumper;
  use lib 'path to module';
  use My::Module;
  
  my $session = My::Module-&gt;new(mysql     =&gt; {
                                              Database =&gt;'module',
                                              Host     =&gt;'10.0.0.2',
                                              Username =&gt;'module',
                                              Password =&gt;'module'
                                             }) or die My::Module-&gt;errstr;
  
  my $dbh = $session-&gt;database();
  my $sth = $dbh-&gt;prepare(q{
               SELECT session_id
               FROM sessions
            });
     $sth-&gt;execute() || die print($dbh-&gt;errstr);
  my $ref = $sth-&gt;fetchall_arrayref({});
  $sth-&gt;finish;
  
  print Dumper \$ref;
  </code></pre>