title: Perl: How to Avoid Globals and Keep Database Connection
body: <p>SO.</p>
  
  <p>Perhaps the title is too general for the specific problem I'm having.</p>
  
  <p>I have a Perl script which connects to a SQLite database. During runtime I perform a lot of database operations such as SELECTs, INSERTs, UPDATEs, etc. I wrapped each database operation in its own subroutine (mainly to hide the SQL statement and any additional processing of the result set).</p>
  
  <p>I want to use the same database handler object every time I need to invoke one of this database operation subroutine, so what I have been doing is to pass the database handler object as an argument. For instance:</p>
  
  <pre><code>my $dbh = DBI-&gt;connect( "dbi:SQLite:dbname=db.db", "", "") or die;
  
  sub select_from_systems {
      my $dbh = shift;
      return $dbh-&gt;selectcol_arrayref("SELECT hostname FROM systems");
  }
  </code></pre>
  
  <p>Just as a side note, I'm using Perl DBI.</p>
  
  <p>I'm just wondering if there's a more clever way to do this so I don't have to pass the database handler object as an argument.</p>
  
  <p>I would like to avoid global variables, I don't feel they are the plague and in this case it may be justified, but still.</p>
  
  <p>Any help or insight is greatly appreciated.</p>
  
  <p>Regards.</p>