Extracting information back out of a DBH

Richard Huxton dev at archonet.com
Tue Feb 10 09:21:09 GMT 2009

Dave Hodgkinson wrote:
> On 10 Feb 2009, at 00:02, Simon Wistow wrote:
>> On Mon, Feb 09, 2009 at 10:24:03PM +0000, peter at dragonstaff.com said:
>>> use DBI;
>>> my $dbh = DBI->connect('dbi:mysql:dbname', 'user', 'pass');
>>> print Dumper( tied(%$dbh) );
>> Ahah, the tied was just the ticket.
> Perl question: what does tied do and why is it used in this situation?


The "tie()" function lets you wrap a built-in type around an object
(e.g. a hash around an object representing the unix-passwd file key=uid,
 value=text). You can then treat it as a hash (bearing in mind that some
operations may be *much* slower than you expect). The object needs to
implement some standard methods to support this.

Looking at "perldoc DBI" says this about "swap_inner_handle"

A DBI handle is a reference to a tied hash. A tied hash has an inner
hash that actually holds the contents.  The swap_inner_handle() method
swaps the inner hashes between two handles. The $h1 and $h2 handles
still point to the same tied hashes, but what those hashes are tied to
has been swapped.  In effect $h1 becomes $h2 and vice-versa. This is
powerful stuff, expect problems. Use with care.

Hmm - not entirely clear to me why a tied hash is being used here, but
then I'm just a simple user of DBI.

Anyway, I think the general opinion on tie-ing arrays, hashes,
filehandles etc is that unless what you've got looks very much like e.g.
a hash already then it's probably not worth the trouble.

  Richard Huxton
  Archonet Ltd

More information about the london.pm mailing list