Multiple choice technology programming languages

Which line of code represents the correct syntax to establish a reference to a database handle?

  1. $dbh = DBI::connect("dbi:mysql:myPhoneBook");
  2. $dbh = DBD:->connect("dbi::mysql::myPhoneBook");
  3. $dbh = DBD::connect("mysql:dbi:myPhoneBook");
  4. $dbh = DBI->connect("dbi:mysql:myPhoneBook");
Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

The correct Perl DBI syntax is $dbh = DBI->connect("dbi:mysql:myPhoneBook"); DBI is the database interface module, and connect is called as a class method using arrow notation. The dbi:mysql: prefix specifies the DBI driver and database name. Options B and C incorrectly use DBD or wrong syntax, and option A omits the arrow operator.

AI explanation

$dbh = DBI->connect("dbi:mysql:myPhoneBook"); is correct. Perl's DBI module is used by calling the class method connect() on the DBI package itself (using the arrow -> for a class method call), passing a data source name string in the form dbi:DriverName:database_name (here dbi:mysql:myPhoneBook), which returns a database handle stored in $dbh. The other options garble this: DBI::connect(...) (double-colon, no arrow) isn't how DBI's connect is invoked as a class method; DBD:->connect and DBD::connect incorrectly call connect on the driver class (DBD::*) instead of DBI, and scramble the DSN format ('mysql:dbi:...' reverses the expected order) — DBD:: modules are the underlying drivers, not the entry point applications call directly.