Solved

Perl beginner - perhaps you forgot to load "DBI"

Posted on 2008-10-18
20
2,343 Views
Last Modified: 2012-05-05
I'm not a Perl programmer and have inherited a Perl script.  I've seen this script run on another machine, so it works and I'd expect any issues running it on my machine to be configuration ones.

When I run the script I get "Can't locate object method "connect" via package "DBI" (perhaps you forgot to load "DBI")

I'm running EPIC under Eclipse on Windows XP.

Checking with the PPM (Perl Package Manager), I see that DBI is present three times:
Package    Area          Installed    Available
DBI            site            1.604         1.602
DBI            perl            1.601         (blank)
DBI            (blank)       (blank)       1.604

What is my problem and how can I correct it?

Thank you.
0
Comment
Question by:josgood
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 8
  • 2
20 Comments
 
LVL 17

Expert Comment

by:mjcoyne
ID: 22750808
Can we see your code?  You should be calling the connect method like this:

$dbh = DBI->connect($dsn, $user, $password, { RaiseError => 1, AutoCommit => 0 });
0
 
LVL 13

Author Comment

by:josgood
ID: 22751986
Here is the line that encounters the problem

#DBD::ODBC driver must be installed prior to try to connect
$g_dbh = DBI->connect('DBI:ODBC:driver=microsoft access driver (*.mdb);dbq='."$database", '', '')
                                    or die ("Could not connect");
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 22752560
Do you have this line prior to trying to connect?

use DBI;
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Author Comment

by:josgood
ID: 22752632
No.  use DBI; is not present.

When I add that line, the script simply terminates.

That has to indicate a problem....

I've seen this script run on the original developer's machine.  I believe I have a configuration problem on mine.

A post on another URL suggested that @inc may not have the correct path.  The same post suggested "which perl" to find the correct path.

I tried running "which perl" in the immediate window, but I can't see how to run a command there.
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 22752795
Your original error would indicate that the script is executing, so you either have the correct path to perl in the shebang line or the web server is configured to locate it for you.

You definitely need to have that use statement.  Since the terminates with its usage, then that would indicate that the DBI module may not be installed correctly.  Can you show us a more complete sample of your code as well as the error message(s) in the web server error log?
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 22752826
Which web server are you using?

One method to verify if the DBI module is installed and Perl can "see" it is to run this command from the XP command prompt.

perl -MDBI -e 1

No output means success,  If you receive any output, then the DBI module needs to be reinstalled.
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 22752855
If this is not a cgi script, then ignore my request for the web server error log entries.

If the script terminates when you include the use DBI; statement but doesn't produce any error message, then the module is probably installed correctly and your script is "dying" prior to the connect statement.  Seeing you code will help us to help you.
0
 
LVL 13

Author Comment

by:josgood
ID: 22752864
>>Which web server are you using?
This script is running locally.  No web server is involved.

>>perl -MDBI -e 1
There was no output.  Here are the lines from the command window
   E:\Perl\bin>perl -MDBI -e 1

   E:\Perl\bin>

The file that encounters the problem is called CRStatisticsConfiguration.pm.  It contains lines such as
   $database = 'D:\InternalProjects\CR Metrics\CRScripts\s5DataBase.mdb'
along with several other variable definitions, followed immediately by
   #DBD::ODBC driver must be installed prior to try to connect
   $g_dbh = DBI->connect('DBI:ODBC:driver=microsoft access driver (*.mdb);dbq='."$database", '', '')
                                       or die ("Could not connect");

Am I correct in thinking that this runs before the first line in my .pl file?  The .pl file has a use DBI;

CRStatisticsConfiguration.pm has only a use Cwd;

0
 
LVL 13

Author Comment

by:josgood
ID: 22752881
Here is an edited copy of the .pm file -- I've removed comments and data definitions that don't apply.  I'm trying to minimize the amount of text you have to wade through.

use Cwd;

# (commentary deleted)
$database = 'D:\InternalProjects\CR Metrics\CRScripts\s5DataBase.mdb';

# ( other data definitions deleted)

#DBD::ODBC driver must be installed prior to try to connect
$g_dbh = DBI->connect('DBI:ODBC:driver=microsoft access driver (*.mdb);dbq='."$database", '', '')
                                    or die ("Could not connect");
0
 
LVL 13

Author Comment

by:josgood
ID: 22752891
I have demonstrated that the .pm file runs before the first line of the .pl file by placing a breakpoint on the first executable line of the .pl file.  That breakpoint was not hit.

use Cwd;
use DBI;
use DBD::ODBC;
use Time::Local;
use DateTime::Format::Excel;
use CRStatisticsConfiguration;
use CRStatisticsFunctions;
use Win32;

# (commentary deleted)

# (breakpoint here was not hit)
$menuID = Win32::MsgBox("Please make sure any opened Excel spreadsheets are closed before running this script!!\n" .
                                    "This script makes use of Excel. Therefore, do not run Excel till this script completes.\n" .
                                     "\t\t\tContinue?",1|MB_ICONEXCLAMATION,"Change Request Statistic Script");
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 22752960
Add the use DBI; statement to the CRStatisticsConfiguration.pm module.
0
 
LVL 13

Author Comment

by:josgood
ID: 22752999
OK.  I just did that.  This is what I tried to do in the first place.

I got the same result as before, which is that the Debug pane shows

(camel) <terminated>CRStatisticsConfiguration.pm(15)[Perl Local]
      (icon)<terminated>Perl Interpreter
      (icon)<terminated, exit value:0>Perl Interpreter

I don't get the "perhaps you forgot to load DBI" message, though.

The console window shows, as text below the tab but not in the clear window,
   <terminated> CRStatisticsConfiguration.pm(17)[Perl Local]Perl Interpreter
Line 17 of CRStatisticsConfiguration.pm, is a blank comment line, as in
   #

I appreciate your help on this...I'm not used to being this lost.
0
 
LVL 17

Expert Comment

by:mjcoyne
ID: 22753188
It's a two part system -- you need the interface (DBI) module installed, and you also need the database driver (DBD) installed (see http://search.cpan.org/~mjevans/DBD-ODBC-1.17/ODBC.pm).

You checked for the presence of the DBI module, but do you have the driver installed?

Also, though I don't use MS Access, your call to load the driver ("DBI:ODBC:driver=microsoft access driver (*.mdb);") looks a little strange...  Could there be an alias or symbolic link on your developer's machine that you don't have on the problematic machine?

0
 
LVL 13

Author Comment

by:josgood
ID: 22753262
I have the ODBC driver installed, as shown in the attached zip file.

>>microsoft access driver (*.mdb)
This name is specified...I don't belief case is relevant.
upload.zip
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 22753521
The connect string is correct for accessing an access db.

The print screens you have show that the OS is configured to connect using ODBC, but that's only half of what you need.  You also need the DBD::ODBC Perl module, which is Perl's database driver needed to access a datasource via ODBC.

Run this test, like you did for the DBI module.

perl -MDBD::ODBC -e 1
0
 
LVL 13

Author Comment

by:josgood
ID: 22753724
I ran the test and there was no output

   E:\Perl\bin>perl -MDBD::ODBC -e 1

   E:\Perl\bin>

One thing that could be tripping us up...I ran this test in a command window.  I'm trying to run the script from Eclipse.

I tried running the script from the command window, as in
   E:\Perl\bin>d:

   D:\>cd D:\InternalProjects\CR Metrics\CRScripts

   D:\InternalProjects\CR Metrics\CRScripts>perl CRStatistics.pl
and the script started to run.

That tells me I have something misconfigured in Eclipse, doesn't it?
0
 
LVL 28

Accepted Solution

by:
FishMonger earned 500 total points
ID: 22754258
It could be a mis configuration with Eclipse, but I've never used it  so I can't say for sure.

Did receive any warnings or errors when you ran: D:\InternalProjects\CR Metrics\CRScripts>perl CRStatistics.pl

The fact that the CRStatisticsConfiguration.pm makes use of the DBI module's method calls without loading the DBI module concerns me.  That fact would indicate to me that the module and accompanying Perl scripts aren't  designed/written properly.
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 22754283
If they're not already included, you should add the following 2 pragmas to your script(s).

use warnings;
use strict;

Those 2 pragmas should be in EVERY Perl script (and module) that you write but beware, if the scripts weren't properly written, the strict pragma could "break" your scripts even more than they already are broken.
0
 
LVL 13

Author Comment

by:josgood
ID: 22754408
I think you've answered my question about as well as it can be answered, so I'm going to close it and will open another if I have followup questions.

>>receive any warnings or errors when you ran: D:\InternalProjects\CR Metrics\CRScripts>perl CRStatistics.pl
No.  I've just started it again and it seems to be running fine.

>>makes use of the DBI module's method calls without loading the DBI module
I don't know Perl, but if loading the module is a requirement, then I'm surprised it runs at all without loading it.   Perhaps we're getting away with this because the main .pl script *does* use DBI.  A confusing point and I get the feeling I'll be coming back to it.

Thank you for your help and your patience.  I'm sure I'll be seeing you on other questions.

Joe
0
 
LVL 13

Author Closing Comment

by:josgood
ID: 31507481
Thank you.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question