tracking database handles

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
The following works, but is it the proper way to store, in this case,
DBI database handles for later use?

  my %handles;

  sub connectdb {
      my $dbh = DBI->connect(...);
      $handles = $dbh;


  for my $v (values %handles) {

I'm looking for general answers on accomplishing this rather than
anything specific to DBI itself.  I initially tried storing the
handles as keys, but that doesn't work for obvious reasons.  Nor does
storing references to the handles as keys.  When trying as references,
I was doing

  $handles = 1;

  for my $k (keys %handles) {

but this results in

  Can't use string ("REF(0x843000c)") as a SCALAR ref while "strict refs"

which makes sense.



Re: tracking database handles

Quoted text here. Click to load it

Yes, but note that this is not threadsafe. If you need threadsafety, see
the Hash::Util::Fieldhash module that comes with perl 5.10.


Re: tracking database handles

Quoted text here. Click to load it

Why use a hash when you don't need hash lookups?

my @handles;
push @handles, $dbh;
foreach (@handles)

Site Timeline