Handle Attributes and Metadata
In addition to methods associated with database and statement handles, the DBI also defines attributes for these handles that allow the developer to examine or fine-tune the environment in which the handles operate. Some attributes are unique to either database or statement handles, and some are common to both.
The attribute values of a handle can be
thought of as a hash of key/value pairs, and can be manipulated
in the same way as you would manipulate an ordinary hash via a
reference. Here are a few examples using the
AutoCommit attribute:
### Set the database handle attribute "AutoCommit" to 1 (e.g., on)
$dbh->{AutoCommit} = 1;
### Fetch the current value of "AutoCommit" from the handle
$foo = $dbh->{AutoCommit};Fetching attributes as hash values, rather than as method calls, has the added bonus that the hash lookup can be interpolated inside double-quoted strings:
### Print the current value of "AutoCommit" from the handle
print "AutoCommit: $dbh->{AutoCommit}\n";With AutoCommit enabled, that would print:
AutoCommit: 1
as you might expect. Actually, since AutoCommit is
a boolean attribute, it would print
1 after any value that Perl considers
true had been assigned to it.
After a false value was assigned, you may
reasonably expect a 0 to be printed, but you might
be surprised to see:
AutoCommit:
That’s because Perl uses an internal representation of
false that is both a numeric zero and an empty string at the same time. When used in a string context, the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access