Richard White’s Perl Note number 1 7 October 2008 Accessing a MySQLdatabasefroma Perl program Assumptions about the Perl version in use These instructions assume that either (i) you are using Perlfrom the Command Prompt on a COMSC Windows machine, or (ii) you have installed ActiveState ActivePerl on your own PC. The COMSC version is currently 5.6.0. (You cannot install a new version of Perl on the COMSC machines because you won’t have Administrator access to C:\Perl.) I have tried installing and running versions 5.6.1 and 5.8.7 on a laptop, and the notes below assume you are using one of these versions. If you are using some other version of Perl, they may or may not work. (Please let me know either way.) You can check which version of Perl you have installed, if any, by opening a Command Prompt. At the prompt, type perl –v or perl -version and press the Enter key. If Perl is installed (correctly) you should see “This is perl, v5.6.0” or similar, followed by several lines of information. Using the MySQL driver Before you can run aPerl program which connects to a database, Perl needs a driver for the particular database software in question. For this module, this is MySQL. Testing for an installed driver The COMSC machines do not have the PerlMySQLdatabase driver installed, nor will a newly installed copy of ActivePerl on your own computer. This section is really so that you can confirm whether you have successfully installed the driver. To test whether you have it, download and run the program DatabaseDemo.pl (or DatabaseTest.pl), which you can get from the Perl folder in Blackboard or from “Richard’s Perl program file repository” on my module web-site, http://users.cs.cf.ac.uk/R.J.White/ If the driver package is not available, the program will report something like: Can't locate DBI.pm in @INC followed by what @INC contains (a list of folders in which Perl searches for packages) and the line number at which your program failed to find the DBI module (DBI.pm) which handles the MySQL driver. Installing the MySQLdatabase driver If you get the error messages described above, there are two ways to install the required driver. Note that there are actually two Perl modules needed: DBI is a generic interface to any type of database; DBD-mysql is the specific driver which lets Perl access MySQL databases. You need both modules. When you have installed them using one of the two methods described below, test again using the program DatabaseDemo.pl or DatabaseTest.pl. There is also a program called listdrivers.pl which (if the DBI module is found) will list the database drivers it can find; these should include the driver mysql. Once you have succeeded in installing the necessary database driver in your network drive H:\My Documents (or wherever on drive H: you stored it when doing Practical 2), if you later use another networked computer in COMSC or anywhere else in the University, you should find that the driver is already be installed and does not need to be installed again. Method 1 – using a Zip file I prepared earlier If you are in the COMSC practical lab., or you are at home and have tried Method 2 but still don’t have a working DatabaseDemo.pl program despite talking really nicely to PPM, try the following method. Because it will install files into your own working folder on the H: drive, whose contents are retained after logging out, after you have done this once it should not need doing again, even if you use a different machine on a future occasion. No guarantees, but I have tested this procedure on a COMSC machine running Perl 5.6.0 and on my laptop, using Perl versions 5.6.1 and 5.8.7 in Windows XP. This working folder be used to contain your Perldatabase programs as well as the PerlMySQLdatabase driver discussed here. (It’s possible, but more complicated, to use different folders.) In my Perl files repository there are two zip archive files called DBI5.6.1.zip and DBI5.8.7.zip. For the Computer Science computers, use DBI5.6.1.zip. Download the appropriate one of these (don’t open it at this stage), depending on which Perl version you installed, from http://users.cs.cf.ac.uk/R.J.White/perl/software/ Double-click on the zip archive file and choose to extract all the files from it. Be careful where the files get stored. Windows XP will try to put them in a new sub- folder called DBI5.6.1\ – don’t let it do this, so you need to change the destination folder path. You want to extract all the files into H:\My Documents or H:\My Documents\Perl Stuff or whatever folder you want to use. Allow it to use the sub-folder names that are stored in the archive (so that all the files get put into the right sub-folders), and allow it to overwrite any files which already exist. Notes: If you are doing this on your own PC, save it in C:\Perl\site\lib or, if this isn’t listed, one of the folders which is listed in @INC. On the COMSC machines you are not allowed to write anything to these folders. The aim is to put them in one of the locations listed in @INC. However, one of the entries in the @INC list should be “.”, which means the current directory, whatever that happens to be. So if you install the drivers in H:\, for example, any Perl program also in H:\ will find the drivers. On my laptop, the PPM had put DBI.pm and several other files into C:\Perl\site\lib where they worked successfully. Packages in this folder will be found by any Perl program wherever it is stored on your computer. There are quite a few files installed by PPM, and I don’t know which are essential and which are not. So I zipped them all into the two zip archive files. If you are short of space or obsessive about tidiness, I think you need to keep the sub- folders auto\, DBI\ and DBD\ which this process creates, but you can delete the sub- folders Bundle\ and MySQL\ If something stops working, extract the files again from the zip archive. If this installation process doesn’t work, try a different workstation in the lab. We have previously found the occasional mis-configured machine. Method 2 – using the PPM This method is better if you can use it, because it is easier and more likely to continue to work when Perl versions or installations change. This method should work for a version of ActivePerl that you have installed on your own PC. It will not work on the COMSC machines, where PPM does not seem to work properly. In that case use “Method 1” above. Install the DBI database interface package and the MySQLdatabase driver DBD- mysql as follows: Make sure you are connected to the Internet, as this procedure will download the packages. If you have installed ActiveState Perl as recommended on your own PC, you should have an entry called “ActiveState ActivePerl 5.6” or “ActivePerl 5.8.7 Build 813” in the All Programs section of your Windows Start menu. Click on that menu item, then on Perl Package Manager. A Command Prompt window will open, and after a delay of as long as two minutes a heading and a “ppm>” prompt will appear. You can type “help”, or just follow these instructions: Type install DBI and then press “y” and the Enter key to say yes to the question. This will fetch the DBI package from somewhere on the Net, install it and print some messages, hopefully ending with “Successfully installed ”. But you haven’t finished yet: install DBD-mysql This will install the DBD-mysql package and print some messages, hopefully also ending with “Successfully installed ”, and finally exit will shut down the Perl Package Manager. Notes on installing DBI and DBD-mysql 1. There are two ways to start the ActiveState Perl Package Manager (PPM) – you can use either. One is to click on its entry in the Start menu as described above (All Programs, ActivePerl ). The other is to open up a Command Prompt window. However, I suspect that both methods end up running the same program - the Start menu method just opens a command prompt and starts the ppm program in it. The command line version can be run in the Command Prompt Window in two ways, either by starting the PPM program without specifying any command by typing just ppm to start the Manager, and then while it is running (at the ppm> prompt, which appears after a long delay) issue the PPM install commands as shown above, or you can issue each command in a separate run of PPM, by typing the full version of the commands, with an initial “ppm ”, which will run each PPM command separately at the Windows command prompt, without requiring “y” to be pressed. For example, ppm install DBI 2. If you can’t run DatabaseDemo.pl or DatabaseTest.pl after trying to install the DBI and the DBD-mysql driver using the Perl Package Manager, make sure you followed the instructions carefully and installed both packages. If you have only installed the DBI package, the Perldatabase test program will say, amongst other things, Can't locate DBD/mysql.pm in @INC This is because the DBI package doesn’t contain the actual driver, that is the instructions specific to a particular database system, such as, in our case, MySQL. You need to install the DBD-mysql package as well, as explained above. 3. You can check which modules are currently installed using ppm query db* which will list all modules starting with “db”, including the two we want. 4. If you get the error message Can't use an undefined value as a hash reference at mysql.pm line 113 then this may be caused by using an older version of DatabaseDemo.pl. Although I tested that version successfully in Linux, it doesn’t work in Windows using the ActivePerl versions described here. I have modified the current version, and DatabaseTest.pl, by adding an additional parameter (consisting of a pair of curly braces, representing an empty hash) at the end of the connect function call. This extra parameter has no effect, and isn’t needed in some versions of the DBD-mysql package, but seems to be required for the ActivePerl versions obtained as described above. You can download the new DatabaseDemo.pl program from Blackboard, by going to the “Perl stuff” folder in Course Documents, or go straight to http://users.cs.cf.ac.uk/R.J.White/perl/ 5. You can un-install the packages so that you can try again, using the commands: ppm uninstall DBD-mysql ppm uninstall DBI When you reinstall: ppm install DBD-mysql then test the DatabaseDemo.pl program after the first package is installed ppm install DBI and test again after the second package is installed. Email both error messages to me if this still doesn't work. 6. PPM thinks it knows where things should be put. In particular, DBI.pm should reside in a directory which is listed in the array @INC. You can list this array from the command line, without creating a whole new Perl program to do it, by typing perl -e 'print join "\n", @INC;' The -e means “Here is a complete Perl program, not a filename”. The join function separates the elements in the list @INC with, in this case, new line characters, so that each directory in @INC is put on a separate line - without this you get a real mess. In Linux, one of these is often /usr/lib/perl5/site_perl, and in Windows C:\Perl\site\lib should be one of the locations. These are the places where Perl will look when it needs to find a module or packages that you are trying to use, such as DBI.pm. You can look to see whether PPM has put the modules DBI.pm into this folder, and mysql.pm into the sub-folder DBD in this folder. Footnote If you’ve been having problems getting the DatabaseDemo.pl program to work, please let me know what worked and what didn’t, otherwise it’s hard to know what’s going wrong. Thanks. . properly. In that case use “Method 1” above. Install the DBI database interface package and the MySQL database driver DBD- mysql as follows: Make sure you are connected to the Internet, as this. contain the actual driver, that is the instructions specific to a particular database system, such as, in our case, MySQL. You need to install the DBD -mysql package as well, as explained above run DatabaseDemo.pl or DatabaseTest.pl after trying to install the DBI and the DBD -mysql driver using the Perl Package Manager, make sure you followed the instructions carefully and installed