style quoting used in this example avoids clashing with quotes that may be used in the SQL statement. Use the double-quote like C operator if you want to interpolate variables into the string. See L for more details. See also the L method, which is used to associate Perl variables with the output columns of a C statement. =head1 THE DBI PACKAGE AND CLASS In this section, we cover the DBI class methods, utility functions, and the dynamic attributes associated with generic DBI handles. =head2 DBI Constants Constants representing the values of the SQL standard types can be imported individually by name, or all together by importing the special C<:sql_types> tag. The names and values of all the defined SQL standard types can be produced like this: foreach (@{ $DBI::EXPORT_TAGS{sql_types} }) { printf "%s=%d\n", $_, &{"DBI::$_"}; } These constants are defined by SQL/CLI, ODBC or both. C has conflicting codes in SQL/CLI and ODBC, DBI uses the ODBC one. See the L, L, and L methods for possible uses. Note that just because the DBI defines a named constant for a given data type doesn't mean that drivers will support that data type. =head2 DBI Class Methods The following methods are provided by the DBI class: =head3 C ($scheme, $driver, $attr_string, $attr_hash, $driver_dsn) = DBI->parse_dsn($dsn) or die "Can't parse DBI DSN '$dsn'"; Breaks apart a DBI Data Source Name (DSN) and returns the individual parts. If $dsn doesn't contain a valid DSN then parse_dsn() returns an empty list. $scheme is the first part of the DSN and is currently always 'dbi'. $driver is the driver name, possibly defaulted to $ENV{DBI_DRIVER}, and may be undefined. $attr_string is the contents of the optional attribute string, which may be undefined. If $attr_string is not empty then $attr_hash is a reference to a hash containing the parsed attribute names and values. $driver_dsn is the last part of the DBI DSN string. For example: ($scheme, $driver, $attr_string, $attr_hash, $driver_dsn) = DBI->parse_dsn("dbi:MyDriver(RaiseError=>1):db=test;port=42"); $scheme = 'dbi'; $driver = 'MyDriver'; $attr_string = 'RaiseError=>1'; $attr_hash = { 'RaiseError' => '1' }; $driver_dsn = 'db=test;port=42'; The parse_dsn() method was added in DBI 1.43. =head3 C $dbh = DBI->connect($data_source, $username, $password) or die $DBI::errstr; $dbh = DBI->connect($data_source, $username, $password, \%attr) or die $DBI::errstr; Establishes a database connection, or session, to the requested C<$data_source>. Returns a database handle object if the connection succeeds. Use C<$dbh-Edisconnect> to terminate the connection. If the connect fails (see below), it returns C and sets both C<$DBI::err> and C<$DBI::errstr>. (It does I explicitly set C<$!>.) You should generally test the return status of C and C if it has failed. Multiple simultaneous connections to multiple databases through multiple drivers can be made via the DBI. Simply make one C call for each database and keep a copy of each returned database handle. The C<$data_source> value must begin with "CIC<:>". The I specifies the driver that will be used to make the connection. (Letter case is significant.) As a convenience, if the C<$data_source> parameter is undefined or empty, the DBI will substitute the value of the environment variable C. If just the I part is empty (i.e., the C<$data_source> prefix is "C"), the environment variable C is used. If neither variable is set, then C dies. Examples of C<$data_source> values are: dbi:DriverName:database_name dbi:DriverName:database_name@hostname:port dbi:DriverName:database=database_name;host=hostname;port=port There is I for the text following the driver name. Each driver is free to use whatever syntax it wants. The only requirement the DBI makes is that all the information is supplied in a single string. You must consult the documentation for the drivers you are using for a description of the syntax they require. It is recommended that drivers support the ODBC style, shown in the last example above. It is also recommended that they support the three common names 'C', 'C', and 'C' (plus 'C' as an alias for C). This simplifies automatic construction of basic DSNs: C<"dbi:$driver:database=$db;host=$host;port=$port">. Drivers should aim to 'do something reasonable' when given a DSN in this form, but if any part is meaningless for that driver (such as 'port' for Informix) it should generate an error if that part is not empty. If the environment variable C is defined (and the driver in C<$data_source> is not "C") then the connect request will automatically be changed to: $ENV{DBI_AUTOPROXY};dsn=$data_source C is typically set as "C". If $ENV{DBI_AUTOPROXY} doesn't begin with 'C' then "dbi:Proxy:" will be prepended to it first. See the DBD::Proxy documentation for more details. If C<$username> or C<$password> are undefined (rather than just empty), then the DBI will substitute the values of the C and C environment variables, respectively. The DBI will warn if the environment variables are not defined. However, the everyday use of these environment variables is not recommended for security reasons. The mechanism is primarily intended to simplify testing. See below for alternative way to specify the username and password. Cconnect> automatically installs the driver if it has not been installed yet. Driver installation either returns a valid driver handle, or it I with an error message that includes the string "C" and the underlying problem. So Cconnect> will die on a driver installation failure and will only return C on a connect failure, in which case C<$DBI::errstr> will hold the error message. Use C if you need to catch the "C" error. The C<$data_source> argument (with the "C" prefix removed) and the C<$username> and C<$password> arguments are then passed to the driver for processing. The DBI does not define any interpretation for the contents of these fields. The driver is free to interpret the C<$data_source>, C<$username>, and C<$password> fields in any way, and supply whatever defaults are appropriate for the engine being accessed. (Oracle, for example, uses the ORACLE_SID and TWO_TASK environment variables if no C<$data_source> is specified.) The C and C attributes for each connection default to "on". (See L and L for more information.) However, it is strongly recommended that you explicitly define C rather than rely on the default. The C attribute defaults to true. The C attribute defaults to false. The C<\%attr> parameter can be used to alter the default settings of C, C, C, and other attributes. For example: $dbh = DBI->connect($data_source, $user, $pass, { PrintError => 0, AutoCommit => 0 }); The username and password can also be specified using the attributes C and C, in which case they take precedence over the C<$username> and C<$password> parameters. You can also define connection attribute values within the C<$data_source> parameter. For example: dbi:DriverName(PrintWarn=>0,PrintError=>0,Taint=>1):... Individual attributes values specified in this way take precedence over any conflicting values specified via the C<\%attr> parameter to C. The C attribute can be used to specify which driver method should be called to establish the connection. The only useful values are 'connect', 'connect_cached', or some specialized case like 'Apache::DBI::connect' (which is automatically the default when running within Apache). Where possible, each session (C<$dbh>) is independent from the transactions in other sessions. This is useful when you need to hold cursors open across transactions--for example, if you use one session for your long lifespan cursors (typically read-only) and another for your short update transactions. For compatibility with old DBI scripts, the driver can be specified by passing its name as the fourth argument to C (instead of C<\%attr>): $dbh = DBI->connect($data_source, $user, $pass, $driver); In this "old-style" form of C, the C<$data_source> should not start with "C". (If it does, the embedded driver_name will be ignored). Also note that in this older form of C, the C<$dbh-E{AutoCommit}> attribute is I, the C<$dbh-E{PrintError}> attribute is off, and the old C environment variable is checked if C is not defined. Beware that this "old-style" C will soon be withdrawn in a future version of DBI. =head3 C $dbh = DBI->connect_cached($data_source, $username, $password) or die $DBI::errstr; $dbh = DBI->connect_cached($data_source, $username, $password, \%attr) or die $DBI::errstr; C is like L, except that the database handle returned is also stored in a hash associated with the given parameters. If another call is made to C with the same parameter values, then the corresponding cached C<$dbh> will be returned if it is still valid. The cached database handle is replaced with a new connection if it has been disconnected or if the C method fails. Note that the behaviour of this method differs in several respects from the behaviour of persistent connections implemented by Apache::DBI. However, if Apache::DBI is loaded then C will use it. Caching connections can be useful in some applications, but it can also cause problems, such as too many connections, and so should be used with care. In particular, avoid changing the attributes of a database handle created via connect_cached() because it will affect other code that may be using the same handle. When connect_cached() returns a handle the attributes will be reset to their initial values. This can cause problems, especially with the C attribute. Also, to ensure that the attributes passed are always the same, avoid passing references inline. For example, the C attribute is specified as a hash reference. Be sure to declare it external to the call to connect_cached(), such that the hash reference is not re-created on every call. A package-level lexical works well: package MyDBH; my $cb = { 'connect_cached.reused' => sub { delete $_[4]->{AutoCommit} }, }; sub dbh { DBI->connect_cached( $dsn, $username, $auth, { Callbacks => $cb }); } Where multiple separate parts of a program are using connect_cached() to connect to the same database with the same (initial) attributes it is a good idea to add a private attribute to the connect_cached() call to effectively limit the scope of the caching. For example: DBI->connect_cached(..., { private_foo_cachekey => "Bar", ... }); Handles returned from that connect_cached() call will only be returned by other connect_cached() call elsewhere in the code if those other calls also pass in the same attribute values, including the private one. (I've used C here as an example, you can use any attribute name with a C prefix.) Taking that one step further, you can limit a particular connect_cached() call to return handles unique to that one place in the code by setting the private attribute to a unique value for that place: DBI->connect_cached(..., { private_foo_cachekey => __FILE__.__LINE__, ... }); By using a private attribute you still get connection caching for the individual calls to connect_cached() but, by making separate database connections for separate parts of the code, the database handles are isolated from any attribute changes made to other handles. The cache can be accessed (and cleared) via the L attribute: my $CachedKids_hashref = $dbh->{Driver}->{CachedKids}; %$CachedKids_hashref = () if $CachedKids_hashref; =head3 C @ary = DBI->available_drivers; @ary = DBI->available_drivers($quiet); Returns a list of all available drivers by searching for C modules through the directories in C<@INC>. By default, a warning is given if some drivers are hidden by others of the same name in earlier directories. Passing a true value for C<$quiet> will inhibit the warning. =head3 C %drivers = DBI->installed_drivers(); Returns a list of driver name and driver handle pairs for all drivers 'installed' (loaded) into the current process. The driver name does not include the 'DBD::' prefix. To get a list of all drivers available in your perl installation you can use L. Added in DBI 1.49. =head3 C DBI->installed_versions; @ary = DBI->installed_versions; $hash = DBI->installed_versions; Calls available_drivers() and attempts to load each of them in turn using install_driver(). For each load that succeeds the driver name and version number are added to a hash. When running under L drivers which appear not be pure-perl are ignored. When called in array context the list of successfully loaded drivers is returned (without the 'DBD::' prefix). When called in scalar context an extra entry for the C is added (and C if appropriate) and a reference to the hash is returned. When called in a void context the installed_versions() method will print out a formatted list of the hash contents, one per line, along with some other information about the DBI version and OS. Due to the potentially high memory cost and unknown risks of loading in an unknown number of drivers that just happen to be installed on the system, this method is not recommended for general use. Use available_drivers() instead. The installed_versions() method is primarily intended as a quick way to see from the command line what's installed. For example: perl -MDBI -e 'DBI->installed_versions' The installed_versions() method was added in DBI 1.38. =head3 C @ary = DBI->data_sources($driver); @ary = DBI->data_sources($driver, \%attr); Returns a list of data sources (databases) available via the named driver. If C<$driver> is empty or C, then the value of the C environment variable is used. The driver will be loaded if it hasn't been already. Note that if the driver loading fails then data_sources() I with an error message that includes the string "C" and the underlying problem. Data sources are returned in a form suitable for passing to the L method (that is, they will include the "C" prefix). Note that many drivers have no way of knowing what data sources might be available for it. These drivers return an empty or incomplete list or may require driver-specific attributes. There is also a data_sources() method defined for database handles. =head3 C DBI->trace($trace_setting) DBI->trace($trace_setting, $trace_filename) DBI->trace($trace_setting, $trace_filehandle) $trace_setting = DBI->trace; The Ctrace> method sets the I trace settings and returns the I trace settings. It can also be used to change where the trace output is sent. There's a similar method, C<$h-Etrace>, which sets the trace settings for the specific handle it's called on. See the L section for full details about the DBI's powerful tracing facilities. =head3 C DBI->visit_handles( $coderef ); DBI->visit_handles( $coderef, $info ); Where $coderef is a reference to a subroutine and $info is an arbitrary value which, if undefined, defaults to a reference to an empty hash. Returns $info. For each installed driver handle, if any, $coderef is invoked as: $coderef->($driver_handle, $info); If the execution of $coderef returns a true value then L is called on that child handle and passed the returned value as $info. For example: my $info = $dbh->{Driver}->visit_child_handles(sub { my ($h, $info) = @_; ++$info->{ $h->{Type} }; # count types of handles (dr/db/st) return $info; # visit kids }); See also L. =head2 DBI Utility Functions In addition to the DBI methods listed in the previous section, the DBI package also provides several utility functions. These can be imported into your code by listing them in the C statement. For example: use DBI qw(neat data_diff); Alternatively, all these utility functions (except hash) can be imported using the C<:utils> import tag. For example: use DBI qw(:utils); =head3 C $description = data_string_desc($string); Returns an informal description of the string. For example: UTF8 off, ASCII, 42 characters 42 bytes UTF8 off, non-ASCII, 42 characters 42 bytes UTF8 on, non-ASCII, 4 characters 6 bytes UTF8 on but INVALID encoding, non-ASCII, 4 characters 6 bytes UTF8 off, undef The initial C on/off refers to Perl's internal SvUTF8 flag. If $string has the SvUTF8 flag set but the sequence of bytes it contains are not a valid UTF-8 encoding then data_string_desc() will report C. The C vs C portion shows C if I the characters in the string are ASCII (have code points <= 127). The data_string_desc() function was added in DBI 1.46. =head3 C $diff = data_string_diff($a, $b); Returns an informal description of the first character difference between the strings. If both $a and $b contain the same sequence of characters then data_string_diff() returns an empty string. For example: Params a & b Result ------------ ------ 'aaa', 'aaa' '' 'aaa', 'abc' 'Strings differ at index 2: a[2]=a, b[2]=b' 'aaa', undef 'String b is undef, string a has 3 characters' 'aaa', 'aa' 'String b truncated after 2 characters' Unicode characters are reported in C<\x{XXXX}> format. Unicode code points in the range U+0800 to U+08FF are unassigned and most likely to occur due to double-encoding. Characters in this range are reported as C<\x{08XX}='C'> where C is the corresponding latin-1 character. The data_string_diff() function only considers logical I and not the underlying encoding. See L for an alternative. The data_string_diff() function was added in DBI 1.46. =head3 C $diff = data_diff($a, $b); $diff = data_diff($a, $b, $logical); Returns an informal description of the difference between two strings. It calls L and L and returns the combined results as a multi-line string. For example, C will return: a: UTF8 off, ASCII, 3 characters 3 bytes b: UTF8 on, non-ASCII, 3 characters 5 bytes Strings differ at index 2: a[2]=c, b[2]=\x{263A} If $a and $b are identical in both the characters they contain I their physical encoding then data_diff() returns an empty string. If $logical is true then physical encoding differences are ignored (but are still reported if there is a difference in the characters). The data_diff() function was added in DBI 1.46. =head3 C $str = neat($value); $str = neat($value, $maxlen); Return a string containing a neat (and tidy) representation of the supplied value. Strings will be quoted, although internal quotes will I be escaped. Values known to be numeric will be unquoted. Undefined (NULL) values will be shown as C (without quotes). If the string is flagged internally as utf8 then double quotes will be used, otherwise single quotes are used and unprintable characters will be replaced by dot (.). For result strings longer than C<$maxlen> the result string will be truncated to C<$maxlen-4> and "C<...'>" will be appended. If C<$maxlen> is 0 or C, it defaults to C<$DBI::neat_maxlen> which, in turn, defaults to 400. This function is designed to format values for human consumption. It is used internally by the DBI for L output. It should typically I be used for formatting values for database use. (See also L.) =head3 C $str = neat_list(\@listref, $maxlen, $field_sep); Calls C on each element of the list and returns a string containing the results joined with C<$field_sep>. C<$field_sep> defaults to C<", ">. =head3 C @bool = looks_like_number(@array); Returns true for each element that looks like a number. Returns false for each element that does not look like a number. Returns C for each element that is undefined or empty. =head3 C $hash_value = DBI::hash($buffer, $type); Return a 32-bit integer 'hash' value corresponding to the contents of $buffer. The $type parameter selects which kind of hash algorithm should be used. For the technically curious, type 0 (which is the default if $type isn't specified) is based on the Perl 5.1 hash except that the value is forced to be negative (for obscure historical reasons). Type 1 is the better "Fowler / Noll / Vo" (FNV) hash. See L for more information. Both types are implemented in C and are very fast. This function doesn't have much to do with databases, except that it can sometimes be handy to store such values in a database. It also doesn't have much to do with perl hashes, like %foo. =head3 C $sts = DBI::sql_type_cast($sv, $sql_type, $flags); sql_type_cast attempts to cast C<$sv> to the SQL type (see L) specified in C<$sql_type>. At present only the SQL types C, C and C are supported. For C the effect is similar to using the value in an expression that requires an integer. It gives the perl scalar an 'integer aspect'. (Technically the value gains an IV, or possibly a UV or NV if the value is too large for an IV.) For C the effect is similar to using the value in an expression that requires a general numeric value. It gives the perl scalar a 'numeric aspect'. (Technically the value gains an NV.) C is similar to C or C but more general and more cautious. It will look at the string first and if it looks like an integer (that will fit in an IV or UV) it will act like C, if it looks like a floating point value it will act like C, if it looks like neither then it will do nothing - and thereby avoid the warnings that would be generated by C and C when given non-numeric data. C<$flags> may be: =over 4 =item C If this flag is specified then when the driver successfully casts the bound perl scalar to a non-string type then the string portion of the scalar will be discarded. =item C If C<$sv> cannot be cast to the requested C<$sql_type> then by default it is left untouched and no error is generated. If you specify C and the cast fails, this will generate an error. =back The returned C<$sts> value is: -2 sql_type is not handled -1 sv is undef so unchanged 0 sv could not be cast cleanly and DBIstcf_STRICT was used 1 sv could not be cast and DBIstcf_STRICT was not used 2 sv was cast successfully This method is exported by the :utils tag and was introduced in DBI 1.611. =head2 DBI Dynamic Attributes Dynamic attributes are always associated with the I (that handle is represented by C<$h> in the descriptions below). Where an attribute is equivalent to a method call, then refer to the method call for all related documentation. Warning: these attributes are provided as a convenience but they do have limitations. Specifically, they have a short lifespan: because they are associated with the last handle used, they should only be used I after calling the method that "sets" them. If in any doubt, use the corresponding method call. =head3 C<$DBI::err> Equivalent to C<$h-Eerr>. =head3 C<$DBI::errstr> Equivalent to C<$h-Eerrstr>. =head3 C<$DBI::state> Equivalent to C<$h-Estate>. =head3 C<$DBI::rows> Equivalent to C<$h-Erows>. Please refer to the documentation for the L method. =head3 C<$DBI::lasth> Returns the DBI object handle used for the most recent DBI method call. If the last DBI method call was a DESTROY then $DBI::lasth will return the handle of the parent of the destroyed handle, if there is one. =head1 METHODS COMMON TO ALL HANDLES The following methods can be used by all types of DBI handles. =head3 C $rv = $h->err; Returns the I database engine error code from the last driver method called. The code is typically an integer but you should not assume that. The DBI resets $h->err to undef before almost all DBI method calls, so the value only has a short lifespan. Also, for most drivers, the statement handles share the same error variable as the parent database handle, so calling a method on one handle may reset the error on the related handles. (Methods which don't reset err before being called include err() and errstr(), obviously, state(), rows(), func(), trace(), trace_msg(), ping(), and the tied hash attribute FETCH() and STORE() methods.) If you need to test for specific error conditions I have your program be portable to different database engines, then you'll need to determine what the corresponding error codes are for all those engines and test for all of them. The DBI uses the value of $DBI::stderr as the C value for internal errors. Drivers should also do likewise. The default value for $DBI::stderr is 2000000000. A driver may return C<0> from err() to indicate a warning condition after a method call. Similarly, a driver may return an empty string to indicate a 'success with information' condition. In both these cases the value is false but not undef. The errstr() and state() methods may be used to retrieve extra information in these cases. See L for more information. =head3 C $str = $h->errstr; Returns the native database engine error message from the last DBI method called. This has the same lifespan issues as the L method described above. The returned string may contain multiple messages separated by newline characters. The errstr() method should not be used to test for errors, use err() for that, because drivers may return 'success with information' or warning messages via errstr() for methods that have not 'failed'. See L for more information. =head3 C $str = $h->state; Returns a state code in the standard SQLSTATE five character format. Note that the specific success code C<00000> is translated to any empty string (false). If the driver does not support SQLSTATE (and most don't), then state() will return C (General Error) for all errors. The driver is free to return any value via C, e.g., warning codes, even if it has not declared an error by returning a true value via the L method described above. The state() method should not be used to test for errors, use err() for that, because drivers may return a 'success with information' or warning state code via state() for methods that have not 'failed'. =head3 C $rv = $h->set_err($err, $errstr); $rv = $h->set_err($err, $errstr, $state); $rv = $h->set_err($err, $errstr, $state, $method); $rv = $h->set_err($err, $errstr, $state, $method, $rv); Set the C, C, and C values for the handle. This method is typically only used by DBI drivers and DBI subclasses. If the L attribute holds a reference to a subroutine it is called first. The subroutine can alter the $err, $errstr, $state, and $method values. See L for full details. If the subroutine returns a true value then the handle C, C, and C values are not altered and set_err() returns an empty list (it normally returns $rv which defaults to undef, see below). Setting C to a I value indicates an error and will trigger the normal DBI error handling mechanisms, such as C and C, if they are enabled, when execution returns from the DBI back to the application. Setting C to C<""> indicates an 'information' state, and setting it to C<"0"> indicates a 'warning' state. Setting C to C also sets C to undef, and C to C<"">, irrespective of the values of the $errstr and $state parameters. The $method parameter provides an alternate method name for the C/C/C/C error string instead of the fairly unhelpful 'C'. The C method normally returns undef. The $rv parameter provides an alternate return value. Some special rules apply if the C or C values for the handle are I set... If C is true then: "C< [err was %s now %s]>" is appended if $err is true and C is already true and the new err value differs from the original one. Similarly "C< [state was %s now %s]>" is appended if $state is true and C is already true and the new state value differs from the original one. Finally "C<\n>" and the new $errstr are appended if $errstr differs from the existing errstr value. Obviously the C<%s>'s above are replaced by the corresponding values. The handle C value is set to $err if: $err is true; or handle C value is undef; or $err is defined and the length is greater than the handle C length. The effect is that an 'information' state only overrides undef; a 'warning' overrides undef or 'information', and an 'error' state overrides anything. The handle C value is set to $state if $state is true and the handle C value was set (by the rules above). Support for warning and information states was added in DBI 1.41. =head3 C $h->trace($trace_settings); $h->trace($trace_settings, $trace_filename); $trace_settings = $h->trace; The trace() method is used to alter the trace settings for a handle (and any future children of that handle). It can also be used to change where the trace output is sent. There's a similar method, Ctrace>, which sets the global default trace settings. See the L section for full details about the DBI's powerful tracing facilities. =head3 C $h->trace_msg($message_text); $h->trace_msg($message_text, $min_level); Writes C<$message_text> to the trace file if the trace level is greater than or equal to $min_level (which defaults to 1). Can also be called as Ctrace_msg($msg)>. See L for more details. =head3 C $h->func(@func_arguments, $func_name) or die ...; The C method can be used to call private non-standard and non-portable methods implemented by the driver. Note that the function name is given as the I argument. It's also important to note that the func() method does not clear a previous error ($DBI::err etc.) and it does not trigger automatic error detection (RaiseError etc.) so you must check the return status and/or $h->err to detect errors. (This method is not directly related to calling stored procedures. Calling stored procedures is currently not defined by the DBI. Some drivers, such as DBD::Oracle, support it in non-portable ways. See driver documentation for more details.) See also install_method() in L for how you can avoid needing to use func() and gain direct access to driver-private methods. =head3 C $is_implemented = $h->can($method_name); Returns true if $method_name is implemented by the driver or a default method is provided by the DBI's driver base class. It returns false where a driver hasn't implemented a method and the default method is provided by the DBI's driver base class is just an empty stub. =head3 C $trace_settings_integer = $h->parse_trace_flags($trace_settings); Parses a string containing trace settings and returns the corresponding integer value used internally by the DBI and drivers. The $trace_settings argument is a string containing a trace level between 0 and 15 and/or trace flag names separated by vertical bar ("C<|>") or comma ("C<,>") characters. For example: C<"SQL|3|foo">. It uses the parse_trace_flag() method, described below, to process the individual trace flag names. The parse_trace_flags() method was added in DBI 1.42. =head3 C $bit_flag = $h->parse_trace_flag($trace_flag_name); Returns the bit flag corresponding to the trace flag name in $trace_flag_name. Drivers are expected to override this method and check if $trace_flag_name is a driver specific trace flags and, if not, then call the DBI's default parse_trace_flag(). The parse_trace_flag() method was added in DBI 1.42. =head3 C $hash_ref = $h->private_attribute_info(); Returns a reference to a hash whose keys are the names of driver-private handle attributes available for the kind of handle (driver, database, statement) that the method was called on. For example, the return value when called with a DBD::Sybase $dbh could look like this: { syb_dynamic_supported => undef, syb_oc_version => undef, syb_server_version => undef, syb_server_version_string => undef, } and when called with a DBD::Sybase $sth they could look like this: { syb_types => undef, syb_proc_status => undef, syb_result_type => undef, } The values should be undef. Meanings may be assigned to particular values in future. =head3 C $rc = $h1->swap_inner_handle( $h2 ); $rc = $h1->swap_inner_handle( $h2, $allow_reparent ); Brain transplants for handles. You don't need to know about this unless you want to become a handle surgeon. A DBI handle is a reference to a tied hash. A tied hash has an I 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 I $h2 and vice-versa. This is powerful stuff, expect problems. Use with care. As a small safety measure, the two handles, $h1 and $h2, have to share the same parent unless $allow_reparent is true. The swap_inner_handle() method was added in DBI 1.44. Here's a quick kind of 'diagram' as a worked example to help think about what's happening: Original state: dbh1o -> dbh1i sthAo -> sthAi(dbh1i) dbh2o -> dbh2i swap_inner_handle dbh1o with dbh2o: dbh2o -> dbh1i sthAo -> sthAi(dbh1i) dbh1o -> dbh2i create new sth from dbh1o: dbh2o -> dbh1i sthAo -> sthAi(dbh1i) dbh1o -> dbh2i sthBo -> sthBi(dbh2i) swap_inner_handle sthAo with sthBo: dbh2o -> dbh1i sthBo -> sthAi(dbh1i) dbh1o -> dbh2i sthAo -> sthBi(dbh2i) =head3 C $h->visit_child_handles( $coderef ); $h->visit_child_handles( $coderef, $info ); Where $coderef is a reference to a subroutine and $info is an arbitrary value which, if undefined, defaults to a reference to an empty hash. Returns $info. For each child handle of $h, if any, $coderef is invoked as: $coderef->($child_handle, $info); If the execution of $coderef returns a true value then C is called on that child handle and passed the returned value as $info. For example: # count database connections with names (DSN) matching a pattern my $connections = 0; $dbh->{Driver}->visit_child_handles(sub { my ($h, $info) = @_; ++$connections if $h->{Name} =~ /foo/; return 0; # don't visit kids }) See also L. =head1 ATTRIBUTES COMMON TO ALL HANDLES These attributes are common to all types of DBI handles. Some attributes are inherited by child handles. That is, the value of an inherited attribute in a newly created statement handle is the same as the value in the parent database handle. Changes to attributes in the new statement handle do not affect the parent database handle and changes to the database handle do not affect existing statement handles, only future ones. Attempting to set or get the value of an unknown attribute generates a warning, except for private driver specific attributes (which all have names starting with a lowercase letter). Example: $h->{AttributeName} = ...; # set/write ... = $h->{AttributeName}; # get/read =head3 C Type: boolean, inherited The C attribute enables useful warnings for certain bad practices. It is enabled by default and should only be disabled in rare circumstances. Since warnings are generated using the Perl C function, they can be intercepted using the Perl C<$SIG{__WARN__}> hook. The C attribute is not related to the C attribute. =head3 C Type: boolean, read-only The C attribute is true if the handle object is "active". This is rarely used in applications. The exact meaning of active is somewhat vague at the moment. For a database handle it typically means that the handle is connected to a database (C<$dbh-Edisconnect> sets C off). For a statement handle it typically means that the handle is a C that may have more data to fetch. (Fetching all the data or calling C<$sth-Efinish> sets C off.) =head3 C Type: boolean The C attribute is true if the handle object has been "executed". Currently only the $dbh do() method and the $sth execute(), execute_array(), and execute_for_fetch() methods set the C attribute. When it's set on a handle it is also set on the parent handle at the same time. So calling execute() on a $sth also sets the C attribute on the parent $dbh. The C attribute for a database handle is cleared by the commit() and rollback() methods (even if they fail). The C attribute of a statement handle is not cleared by the DBI under any circumstances and so acts as a permanent record of whether the statement handle was ever used. The C attribute was added in DBI 1.41. =head3 C Type: integer, read-only For a driver handle, C is the number of currently existing database handles that were created from that driver handle. For a database handle, C is the number of currently existing statement handles that were created from that database handle. For a statement handle, the value is zero. =head3 C Type: integer, read-only Like C, but only counting those that are C (as above). =head3 C Type: hash ref For a database handle, C returns a reference to the cache (hash) of statement handles created by the L method. For a driver handle, returns a reference to the cache (hash) of database handles created by the L method. =head3 C Type: scalar, read-only The C attribute identifies the type of a DBI handle. Returns "dr" for driver handles, "db" for database handles and "st" for statement handles. =head3 C Type: array ref The ChildHandles attribute contains a reference to an array of all the handles created by this handle which are still accessible. The contents of the array are weak-refs and will become undef when the handle goes out of scope. (They're cleared out occasionally.) C returns undef if your perl version does not support weak references (check the L module). The referenced array returned should be treated as read-only. For example, to enumerate all driver handles, database handles and statement handles: sub show_child_handles { my ($h, $level) = @_; printf "%sh %s %s\n", $h->{Type}, "\t" x $level, $h; show_child_handles($_, $level + 1) for (grep { defined } @{$h->{ChildHandles}}); } my %drivers = DBI->installed_drivers(); show_child_handles($_, 0) for (values %drivers); =head3 C Type: boolean, inherited The C attribute is used by emulation layers (such as Oraperl) to enable compatible behaviour in the underlying driver (e.g., DBD::Oracle) for this handle. Not normally set by application code. It also has the effect of disabling the 'quick FETCH' of attribute values from the handles attribute cache. So all attribute values are handled by the drivers own FETCH method. This makes them slightly slower but is useful for special-purpose drivers like DBD::Multiplex. =head3 C Type: boolean The default value, false, means a handle will be fully destroyed as normal when the last reference to it is removed, just as you'd expect. If set true then the handle will be treated by the DESTROY as if it was no longer Active, and so the I related effects of DESTROYing a handle will be skipped. Think of the name as meaning 'treat the handle as not-Active in the DESTROY method'. For a database handle, this attribute does not disable an I call to the disconnect method, only the implicit call from DESTROY that happens if the handle is still marked as C. This attribute is specifically designed for use in Unix applications that "fork" child processes. For some drivers, when the child process exits the destruction of inherited handles cause the corresponding handles in the parent process to cease working. Either the parent or the child process, but not both, should set C true on all their shared handles. Alternatively, and preferably, the L can be set in the parent on connect. To help tracing applications using fork the process id is shown in the trace log whenever a DBI or handle trace() method is called. The process id also shown for I method call if the DBI trace level (not handle trace level) is set high enough to show the trace from the DBI's method dispatcher, e.g. >= 9. =head3 C Type: boolean, inherited The L attribute, described above, needs to be explicitly set in the child process after a fork(), on every active database and statement handle. This is a problem if the code that performs the fork() is not under your control, perhaps in a third-party module. Use C to get around this situation. If set true, the DESTROY method will check the process id of the handle and, if different from the current process id, it will set the I attribute. It is strongly recommended that C is enabled on all new code (it's only not enabled by default to avoid backwards compatibility problems). This is the example it's designed to deal with: my $dbh = DBI->connect(...); some_code_that_forks(); # Perhaps without your knowledge # Child process dies, destroying the inherited dbh $dbh->do(...); # Breaks because parent $dbh is now broken The C attribute was added in DBI 1.614. =head3 C Type: boolean, inherited The C attribute controls the printing of warnings recorded by the driver. When set to a true value (the default) the DBI will check method calls to see if a warning condition has been set. If so, the DBI will effectively do a C where C<$class> is the driver class and C<$method> is the name of the method which failed. E.g., DBD::Oracle::db execute warning: ... warning text here ... If desired, the warnings can be caught and processed using a C<$SIG{__WARN__}> handler or modules like CGI::Carp and CGI::ErrorWrap. See also L for how warnings are recorded and L for how to influence it. Fetching the full details of warnings can require an extra round-trip to the database server for some drivers. In which case the driver may opt to only fetch the full details of warnings if the C attribute is true. If C is false then these drivers should still indicate the fact that there were warnings by setting the warning string to, for example: "3 warnings". =head3 C Type: boolean, inherited The C attribute can be used to force errors to generate warnings (using C) in addition to returning error codes in the normal way. When set "on", any method which results in an error occurring will cause the DBI to effectively do a C where C<$class> is the driver class and C<$method> is the name of the method which failed. E.g., DBD::Oracle::db prepare failed: ... error text here ... By default, Cconnect> sets C "on". If desired, the warnings can be caught and processed using a C<$SIG{__WARN__}> handler or modules like CGI::Carp and CGI::ErrorWrap. =head3 C Type: boolean, inherited The C attribute can be used to force warnings to raise exceptions rather then simply printing them. It is "off" by default. When set "on", any method which sets warning condition will cause the DBI to effectively do a C, where C<$class> is the driver class and C<$method> is the name of the method that sets warning condition. E.g., DBD::Oracle::db execute warning: ... warning text here ... If you turn C on then you'd normally turn C off. If C is also on, then the C is done first (naturally). This attribute was added in DBI 1.643. =head3 C Type: boolean, inherited The C attribute can be used to force errors to raise exceptions rather than simply return error codes in the normal way. It is "off" by default. When set "on", any method which results in an error will cause the DBI to effectively do a C, where C<$class> is the driver class and C<$method> is the name of the method that failed. E.g., DBD::Oracle::db prepare failed: ... error text here ... If you turn C on then you'd normally turn C off. If C is also on, then the C is done first (naturally). Typically C is used in conjunction with C, or a module like L or L, to catch the exception that's been thrown and handle it. For example: use Try::Tiny; try { ... $sth->execute(); ... } catch { # $sth->err and $DBI::err will be true if error was from DBI warn $_; # print the error (which Try::Tiny puts into $_) ... # do whatever you need to deal with the error }; In the catch block the $DBI::lasth variable can be useful for diagnosis and reporting if you can't be sure which handle triggered the error. For example, $DBI::lasth->{Type} and $DBI::lasth->{Statement}. See also L. If you want to temporarily turn C off (inside a library function that is likely to fail, for example), the recommended way is like this: { local $h->{RaiseError}; # localize and turn off for this block ... } The original value will automatically and reliably be restored by Perl, regardless of how the block is exited. The same logic applies to other attributes, including C. =head3 C Type: code ref, inherited The C attribute can be used to provide your own alternative behaviour in case of errors. If set to a reference to a subroutine then that subroutine is called when an error is detected (at the same point that C and C are handled). It is called also when C is enabled and a warning is detected. The subroutine is called with three parameters: the error message string that C, C or C would use, the DBI handle being used, and the first value being returned by the method that failed (typically undef). If the subroutine returns a false value then the C, C and/or C attributes are checked and acted upon as normal. For example, to C with a full stack trace for any error: use Carp; $h->{HandleError} = sub { confess(shift) }; Or to turn errors into exceptions: use Exception; # or your own favourite exception module $h->{HandleError} = sub { Exception->new('DBI')->raise($_[0]) }; It is possible to 'stack' multiple HandleError handlers by using closures: sub your_subroutine { my $previous_handler = $h->{HandleError}; $h->{HandleError} = sub { return 1 if $previous_handler and &$previous_handler(@_); ... your code here ... }; } Using a C inside a subroutine to store the previous C value is important. See L and L for more information about I. It is possible for C to alter the error message that will be used by C, C and C if it returns false. It can do that by altering the value of $_[0]. This example appends a stack trace to all errors and, unlike the previous example using Carp::confess, this will work C as well as C: $h->{HandleError} = sub { $_[0]=Carp::longmess($_[0]); 0; }; It is also possible for C to hide an error, to a limited degree, by using L to reset $DBI::err and $DBI::errstr, and altering the return value of the failed method. For example: $h->{HandleError} = sub { return 0 unless $_[0] =~ /^\S+ fetchrow_arrayref failed:/; return 0 unless $_[1]->err == 1234; # the error to 'hide' $h->set_err(undef,undef); # turn off the error $_[2] = [ ... ]; # supply alternative return value return 1; }; This only works for methods which return a single value and is hard to make reliable (avoiding infinite loops, for example) and so isn't recommended for general use! If you find a I use for it then please let me know. =head3 C Type: code ref, inherited The C attribute can be used to intercept the setting of handle C, C, and C values. If set to a reference to a subroutine then that subroutine is called whenever set_err() is called, typically by the driver or a subclass. The subroutine is called with five arguments, the first five that were passed to set_err(): the handle, the C, C, and C values being set, and the method name. These can be altered by changing the values in the @_ array. The return value affects set_err() behaviour, see L for details. It is possible to 'stack' multiple HandleSetErr handlers by using closures. See L for an example. The C and C subroutines differ in subtle but significant ways. HandleError is only invoked at the point where the DBI is about to return to the application with C set true. It's not invoked by the failure of a method that's been called by another DBI method. HandleSetErr, on the other hand, is called whenever set_err() is called with a defined C value, even if false. So it's not just for errors, despite the name, but also warn and info states. The set_err() method, and thus HandleSetErr, may be called multiple times within a method and is usually invoked from deep within driver code. In theory a driver can use the return value from HandleSetErr via set_err() to decide whether to continue or not. If set_err() returns an empty list, indicating that the HandleSetErr code has 'handled' the 'error', the driver could then continue instead of failing (if that's a reasonable thing to do). This isn't excepted to be common and any such cases should be clearly marked in the driver documentation and discussed on the dbi-dev mailing list. The C attribute was added in DBI 1.41. =head3 C Type: unsigned integer The C attribute is incremented whenever the set_err() method records an error. It isn't incremented by warnings or information states. It is not reset by the DBI at any time. The C attribute was added in DBI 1.41. Older drivers may not have been updated to use set_err() to record errors and so this attribute may not be incremented when using them. =head3 C Type: boolean, inherited The C attribute can be used to cause the relevant Statement text to be appended to the error messages generated by the C, C, C and C attributes. Only applies to errors on statement handles plus the prepare(), do(), and the various C database handle methods. (The exact format of the appended text is subject to change.) If C<$h-E{ParamValues}> returns a hash reference of parameter (placeholder) values then those are formatted and appended to the end of the Statement text in the error message. =head3 C Type: integer, inherited The C attribute can be used as an alternative to the L method to set the DBI trace level and trace flags for a specific handle. See L for more details. The C attribute is especially useful combined with C to alter the trace settings for just a single block of code. =head3 C Type: string, inherited The C attribute is used to specify whether the fetchrow_hashref() method should perform case conversion on the field names used for the hash keys. For historical reasons it defaults to 'C' but it is recommended to set it to 'C' (convert to lower case) or 'C' (convert to upper case) according to your preference. It can only be set for driver and database handles. For statement handles the value is frozen when prepare() is called. =head3 C Type: boolean, inherited The C attribute can be used to control the trimming of trailing space characters from fixed width character (CHAR) fields. No other field types are affected, even where field values have trailing spaces. The default is false (although it is possible that the default may change). Applications that need specific behaviour should set the attribute as needed. Drivers are not required to support this attribute, but any driver which does not support it must arrange to return C as the attribute value. =head3 C Type: unsigned integer, inherited The C attribute may be used to control the maximum length of 'long' type fields (LONG, BLOB, CLOB, MEMO, etc.) which the driver will read from the database automatically when it fetches each row of data. The C attribute only relates to fetching and reading long values; it is not involved in inserting or updating them. A value of 0 means not to automatically fetch any long data. Drivers may return undef or an empty string for long fields when C is 0. The default is typically 0 (zero) or 80 bytes but may vary between drivers. Applications fetching long fields should set this value to slightly larger than the longest long field value to be fetched. Some databases return some long types encoded as pairs of hex digits. For these types, C relates to the underlying data length and not the doubled-up length of the encoded string. Changing the value of C for a statement handle after it has been C'd will typically have no effect, so it's common to set C on the C<$dbh> before calling C. For most drivers the value used here has a direct effect on the memory used by the statement handle while it's active, so don't be too generous. If you can't be sure what value to use you could execute an extra select statement to determine the longest value. For example: $dbh->{LongReadLen} = $dbh->selectrow_array(qq{ SELECT MAX(OCTET_LENGTH(long_column_name)) FROM table WHERE ... }); $sth = $dbh->prepare(qq{ SELECT long_column_name, ... FROM table WHERE ... }); You may need to take extra care if the table can be modified between the first select and the second being executed. You may also need to use a different function if OCTET_LENGTH() does not work for long types in your database. For example, for Sybase use DATALENGTH() and for Oracle use LENGTHB(). See also L for information on truncation of long types. =head3 C Type: boolean, inherited The C attribute may be used to control the effect of fetching a long field value which has been truncated (typically because it's longer than the value of the C attribute). By default, C is false and so fetching a long value that needs to be truncated will cause the fetch to fail. (Applications should always be sure to check for errors after a fetch loop in case an error, such as a divide by zero or long field truncation, caused the fetch to terminate prematurely.) If a fetch fails due to a long field truncation when C is false, many drivers will allow you to continue fetching further rows. See also L. =head3 C Type: boolean, inherited If the C attribute is set to a true value I Perl is running in taint mode (e.g., started with the C<-T> option), then all the arguments to most DBI method calls are checked for being tainted. I The attribute defaults to off, even if Perl is in taint mode. See L for more about taint mode. If Perl is not running in taint mode, this attribute has no effect. When fetching data that you trust you can turn off the TaintIn attribute, for that statement handle, for the duration of the fetch loop. The C attribute was added in DBI 1.31. =head3 C Type: boolean, inherited If the C attribute is set to a true value I Perl is running in taint mode (e.g., started with the C<-T> option), then most data fetched from the database is considered tainted. I The attribute defaults to off, even if Perl is in taint mode. See L for more about taint mode. If Perl is not running in taint mode, this attribute has no effect. When fetching data that you trust you can turn off the TaintOut attribute, for that statement handle, for the duration of the fetch loop. Currently only fetched data is tainted. It is possible that the results of other DBI method calls, and the value of fetched attributes, may also be tainted in future versions. That change may well break your applications unless you take great care now. If you use DBI Taint mode, please report your experience and any suggestions for changes. The C attribute was added in DBI 1.31. =head3 C Type: boolean, inherited The C attribute is a shortcut for L and L (it is also present for backwards compatibility). Setting this attribute sets both L and L, and retrieving it returns a true value if and only if L and L are both set to true values. =head3 C Type: inherited The C attribute enables the collection and reporting of method call timing statistics. See the L module documentation for I more detail. The C attribute was added in DBI 1.24. =head3 C Type: boolean, inherited An application can set the C attribute of a handle to a true value to indicate that it will not be attempting to make any changes using that handle or any children of it. Note that the exact definition of 'read only' is rather fuzzy. For more details see the documentation for the driver you're using. If the driver can make the handle truly read-only then it should (unless doing so would have unpleasant side effect, like changing the consistency level from per-statement to per-session). Otherwise the attribute is simply advisory. A driver can set the C attribute itself to indicate that the data it is connected to cannot be changed for some reason. If the driver cannot ensure the C attribute is adhered to it will record a warning. In this case reading the C attribute back after it is set true will return true even if the underlying driver cannot ensure this (so any application knows the application declared itself ReadOnly). Library modules and proxy drivers can use the attribute to influence their behavior. For example, the DBD::Gofer driver considers the C attribute when making a decision about whether to retry an operation that failed. The attribute should be set to 1 or 0 (or undef). Other values are reserved. =head3 C Type: hash ref The DBI callback mechanism lets you intercept, and optionally replace, any method call on a DBI handle. At the extreme, it lets you become a puppet master, deceiving the application in any way you want. The C attribute is a hash reference where the keys are DBI method names and the values are code references. For each key naming a method, the DBI will execute the associated code reference before executing the method. The arguments to the code reference will be the same as to the method, including the invocant (a database handle or statement handle). For example, say that to callback to some code on a call to C: $dbh->{Callbacks} = { prepare => sub { my ($dbh, $query, $attrs) = @_; print "Preparing q{$query}\n" }, }; The callback would then be executed when you called the C method: $dbh->prepare('SELECT 1'); And the output of course would be: Preparing q{SELECT 1} Because callbacks are executed I the methods they're associated with, you can modify the arguments before they're passed on to the method call. For example, to make sure that all calls to C are immediately prepared by L, add a callback that makes sure that the C attribute is always set: my $dbh = DBI->connect($dsn, $username, $auth, { Callbacks => { prepare => sub { $_[2] ||= {}; $_[2]->{pg_prepare_now} = 1; return; # must return nothing }, } }); Note that we are editing the contents of C<@_> directly. In this case we've created the attributes hash if it's not passed to the C call. You can also prevent the associated method from ever executing. While a callback executes, C<$_> holds the method name. (This allows multiple callbacks to share the same code reference and still know what method was called.) To prevent the method from executing, simply C. For example, if you wanted to disable calls to C, you could do this: $dbh->{Callbacks} = { ping => sub { # tell dispatch to not call the method: undef $_; # return this value instead: return "42 bells"; } }; As with other attributes, Callbacks can be specified on a handle or via the attributes to C. Callbacks can also be applied to a statement methods on a statement handle. For example: $sth->{Callbacks} = { execute => sub { print "Executing ", shift->{Statement}, "\n"; } }; The C attribute of a database handle isn't copied to any statement handles it creates. So setting callbacks for a statement handle requires you to set the C attribute on the statement handle yourself, as in the example above, or use the special C key described below. B In addition to DBI handle method names, the C hash reference supports four additional keys. The first is the C key. When a statement handle is created from a database handle the C key of the database handle's C attribute, if any, becomes the new C attribute of the statement handle. This allows you to define callbacks for all statement handles created from a database handle. For example, if you wanted to count how many times C was called in your application, you could write: my $exec_count = 0; my $dbh = DBI->connect( $dsn, $username, $auth, { Callbacks => { ChildCallbacks => { execute => sub { $exec_count++; return; } } } }); END { print "The execute method was called $exec_count times\n"; } The other three special keys are C, C, and C. These keys define callbacks that are called when C is called, but allow different behaviors depending on whether a new handle is created or a handle is returned. The callback is invoked with these arguments: C<$dbh, $dsn, $user, $auth, $attr>. For example, some applications uses C to connect with C enabled and then disable C temporarily for transactions. If C is called during a transaction, perhaps in a utility method, then it might select the same cached handle and then force C on, forcing a commit of the transaction. See the L documentation for one way to deal with that. Here we'll describe an alternative approach using a callback. Because the C and C callbacks are invoked before C has applied the connect attributes, you can use them to edit the attributes that will be applied. To prevent a cached handle from having its transactions committed before it's returned, you can eliminate the C attribute in a C callback, like so: my $cb = { 'connect_cached.reused' => sub { delete $_[4]->{AutoCommit} }, }; sub dbh { my $self = shift; DBI->connect_cached( $dsn, $username, $auth, { PrintError => 0, RaiseError => 1, AutoCommit => 1, Callbacks => $cb, }); } The upshot is that new database handles are created with C enabled, while cached database handles are left in whatever transaction state they happened to be in when retrieved from the cache. Note that we've also used a lexical for the callbacks hash reference. This is because C returns a new database handle if any of the attributes passed to is have changed. If we used an inline hash reference, C would return a new database handle every time. Which would rather defeat the purpose. A more common application for callbacks is setting connection state only when a new connection is made (by connect() or connect_cached()). Adding a callback to the connected method (when using C) or via C (when useing connect_cached()>) makes this easy. The connected() method is a no-op by default (unless you subclass the DBI and change it). The DBI calls it to indicate that a new connection has been made and the connection attributes have all been set. You can give it a bit of added functionality by applying a callback to it. For example, to make sure that MySQL understands your application's ANSI-compliant SQL, set it up like so: my $dbh = DBI->connect($dsn, $username, $auth, { Callbacks => { connected => sub { shift->do(q{ SET SESSION sql_mode='ansi,strict_trans_tables,no_auto_value_on_zero'; }); return; }, } }); If you're using C, use the C callback, instead. This is because C is called for both new and reused database handles, but you want to execute a callback only the when a new database handle is returned. For example, to set the time zone on connection to a PostgreSQL database, try this: my $cb = { 'connect_cached.connected' => sub { shift->do('SET timezone = UTC'); } }; sub dbh { my $self = shift; DBI->connect_cached( $dsn, $username, $auth, { Callbacks => $cb }); } One significant limitation with callbacks is that there can only be one per method per handle. This means it's easy for one use of callbacks to interfere with, or typically simply overwrite, another use of callbacks. For this reason modules using callbacks should document the fact clearly so application authors can tell if use of callbacks by the module will clash with use of callbacks by the application. You might be able to work around this issue by taking a copy of the original callback and calling it within your own. For example: my $prev_cb = $h->{Callbacks}{method_name}; $h->{Callbacks}{method_name} = sub { if ($prev_cb) { my @result = $prev_cb->(@_); return @result if not $_; # $prev_cb vetoed call } ... your callback logic here ... }; =head3 C The DBI provides a way to store extra information in a DBI handle as "private" attributes. The DBI will allow you to store and retrieve any attribute which has a name starting with "C". It is I recommended that you use just I private attribute (e.g., use a hash ref) I