Connecting with Attributes
One of Perl’s many catch phrases is
"there’s more than one way to do it,” and the DBI is no exception. In addition to
being able to set attributes on a handle by simple assignment and by
the attribute parameter of the
connect()
method (as shown earlier), the DBI provides another way.
You
can include attribute assignments in the
data source name parameter of the connect()
method. For example:
$dbh = DBI->connect( "dbi:Oracle:archaeo", "username", "password" , {
RaiseError => 1
});can also be expressed as:
$dbh = DBI->connect( "dbi:Oracle(RaiseError=>1):archaeo", '', '');
You can’t have any space before the opening parenthesis or
after the closing one before the colon, but you can have spaces
within the parentheses. You can also use just =
instead of => if you prefer. If you want to set
more than one attribute then use a comma to separate each one.
The attribute settings in the data source name parameter take
precedence over those in the attribute parameter. This can be very
handy when you want to override a hardcoded attribute setting, such
as PrintError. For example, this code will leave
PrintError on:
$dbh = DBI->connect( "dbi:Oracle(PrintError=>1):archaeo", '', '', {
PrintError => 0
});But what’s the point of just hardcoding the attribute setting in two different places? This example is not very useful as it stands, but we could let the application accept the data source name parameter from the command line as an option, or leave it empty and use the ...