-1

I've installed DBI module via cpan. CPAN has been configured to use local directory, so I have ~/perl5 and ~/.cpan directories. The module apparently is in ~/.cpan/build/DBI-1.642-0, which in fact does have DBI.pm file there.

However, when I execute the following command as a test, the command suggests there is no "connect" object:

$ perl -e 'use lib qw( .cpan/build/DBI-1.642-0/ ); DBI->connect("dbi:SQLite:dbname=foo.sqlite","","");'
Can't locate object method "connect" via package "DBI" (perhaps you forgot to load "DBI"?) at -e line 1.

Environment: Debian-based distribution, perl 5.26.2 .

Note on possible duplicates:


Addendum: From the discussion in the comments, it's clear that a lot of users focus on just use DBI statement. As I've mentioned in the comments:

The whole goal is to make use of DBI module installed via cpan. Prior to installing DBD::SQLite neither use lib nor use DBI were giving a working solution. So the suggestion use DBI by itself was not useful.

Please note, I have tried both use DBI and use lib qw() methods prior to asking the question. The use DBI line by itself was not effective without installing DBD::SQLite module as mentioned in my answer.

Sergiy Kolodyazhnyy
  • 938
  • 1
  • 13
  • 41
  • 2
    You have to `use DBI` or add `-MDBI` to your command line. That build folder is for building, not the place where it got installed. Did you also install DBD::SQLite and your OS's `libsqlite3-dev` package? If you used `cpan` to install it, you don't need a `use lib` at all. Try `$ perl -MDBI -e 'DBI->connect("dbi:SQLite:dbname=foo.sqlite");'`. – simbabque Jan 22 '19 at 10:44
  • @simbabque The `use DBI` calls system-wide DBI module from `@INC` list of directories, which apparently on Debian based systems is missing SQLite. In any case, `use DBI` produces another error: `install_driver(SQLite) failed: Can't locate DBD/SQLite.pm` Same occurs with `perl -MDBI ...` suggestion – Sergiy Kolodyazhnyy Jan 22 '19 at 10:50
  • 3
    DBI and SQLite have nothing to do with each other. They are different modules. If you're using the system Perl however, you should use your package manager to install both DBI and SQLite. https://packages.debian.org/jessie/libdbi-perl and https://packages.debian.org/jessie/libdbd-sqlite3-perl. – simbabque Jan 22 '19 at 11:19
  • @simbabque Thank you for the suggestion. On my personal laptop, I can install both debian packages of course, but when I'm gonna work over ssh on different system I'm not gonna have access to root level privileges and installing packages via system package manager. So hopefully it makes more sense why I'm trying to deal with cpan. – Sergiy Kolodyazhnyy Jan 22 '19 at 11:23
  • 1
    In that case, you would want to look at Perl's local::lib module, which allows you to control what to install where. But for SQLite to work you still need the libsqlite3-dev package. – simbabque Jan 22 '19 at 11:35
  • @simbabque `libsqlite3-dev` might be already installed. I'll have to verify that later. Thanks for the suggestion on `local::lib`. – Sergiy Kolodyazhnyy Jan 22 '19 at 11:55
  • @simbabque DBD::SQLite uses its own copy of sqlite3.c, not a system library. – Shawn Jan 22 '19 at 12:19

2 Answers2

4

Converted from a command line script to an actual program, your code looks like this:

use lib qw( .cpan/build/DBI-1.642-0/ );

DBI->connect("dbi:SQLite:dbname=foo.sqlite","","");

And the error message you get is:

Can't locate object method "connect" via package "DBI" (perhaps you forgot to load "DBI"?) at -e line 1.

That error is pretty clear.

(perhaps you forgot to load "DBI"?)

The problem here is that you are missing the line of code which actually loads the DBI module. You need to add this:

use DBI;

Your use lib qw( .cpan/build/DBI-1.642-0/ ) line is very strange. You're asking Perl to load the module from the temporary build directory that was used during the installation. That's not the version that you want to use at all. When cpan has finished its work, you will have a version of DBI installed in your standard Perl library directories that you will be able to access without the need for any use lib code.

I'll also add that if you're using the system-installed version of Perl, there's no need to use cpan to install the most popular modules. You can use your distribution's repository of pre-built packages. For example apt get install libdbi-perl (on Debian and similar) or dnf install perl-DBI (on Red Hat).

In your answer, you have silently added the missing use DBI statement and you claim that installing DBD::SQLite solved your problem. That may have solved a different problem that you had, but it didn't solve the problem in your original question.

Dave Cross
  • 68,119
  • 3
  • 51
  • 97
  • While I agree that it's better to use distribution's repository, I cannot use package manager on a system where I don't have `sudo` access, hence using `cpan`. Also, in my answer I haven't just silently added `use DBI`. I've mentioned multiple times already: prior to installing `DBD::SQLite` the statement did not work. After installation, code works as intended. I thank you for the answer and I agree with multiple points. Perhaps my situation is a corner case, but I've documented my steps in the answer as is. As for the question, if you have suggestion on where it's unclear, please suggest – Sergiy Kolodyazhnyy Jan 22 '19 at 12:11
0

Issue with perl -e has been resolved, since apparently SQLite was not installed. I had to open cpan shell and run install DBD::SQLite. Now, the command-line works properly:

$ $ perl -e 'use DBI; my $db = DBI->connect( "dbi:SQLite:dbname=foo.sqlite","","" );my $stmt = qq(CREATE TABLE foo(a int, b text); ); $db->do($stmt)'
$ sqlite3 foo.sqlite
SQLite version 3.23.1 2018-04-10 17:39:29
Enter ".help" for usage hints.
sqlite> .tables
foo
sqlite> .schema foo
CREATE TABLE foo(a int, b text);
sqlite> 

As for the module itself, it has been installed in ~/perl5/lib/perl5/x86_64-linux-gnu-thread-multi/DBD/ directory.


Based on Berserk's answer, the following also works for explicitly calling the :

$ perl -e 'use lib qw(  /home/user/perl5/x86_64-linux-gnu-thread-multi/DBD ); use DBI; my $db = DBI->connect( "dbi:SQLite:dbname=foo.sqlite","","" );my $stmt = qq(CREATE TABLE foo(a int, b text); ); $db->do($stmt)'

To ensure this uses the library declaration from use lib qw() explicitly, I've also cleared the @inc array in some of my tests.

Sergiy Kolodyazhnyy
  • 938
  • 1
  • 13
  • 41
  • 1
    `/home/user/perl5/x86_64-linux-gnu-thread-multi/DBD` is a wrong directory and the change you observe cannot relate to this directory. Maybe ( _maybe_ ) `/home/user/perl5/x86_64-linux-gnu-thread-multi/` would be a correct directory. – Corion Jan 22 '19 at 11:30
  • The problem in your original question was not caused by DBD::SQLite being missing. The problem in your original question was caused by you not having `use DBI` in your code. – Dave Cross Jan 22 '19 at 11:36
  • @DaveCross As I've mentioned in the comments under the question, I tried `use DBI` as well. The assumption was that `use DBI` calls the system-wide `DBI` module, which may have been wrong. So, the issue was in fact `DBD::SQLite`. Before installing - errors were thrown, after installing - works as intended. – Sergiy Kolodyazhnyy Jan 22 '19 at 11:50
  • @Corion You might be correct, though I'm pretty sure I copied the code exactly, and even added `@inc=()` to ensure that the directory works. I will try to verify that later - currently have to go off-line. Still, thanks for the comment. – Sergiy Kolodyazhnyy Jan 22 '19 at 11:54
  • @SergiyKolodyazhnyy: And as that comment says, when you added `use DBI`, the error changed. So adding `use DBI` fixed your original error and revealed another, different, error. – Dave Cross Jan 22 '19 at 11:54
  • @DaveCross Perhaps my question is a bit unclear, as it's being misunderstood. The whole goal is to make use of `DBI` module installed via `cpan`. Prior to installing `DBD::SQLite` neither `use lib` nor `use DBI` were giving a working solution. So the suggestion `use DBI` by itself was not useful. – Sergiy Kolodyazhnyy Jan 22 '19 at 12:00
  • @SergiyKolodyazhnyy: We were concentrating on the `use DBI` line because that's what solves the error mentioned in the title of your question. If your question had included something like "I tried adding `use DBI` and the error changed to this..." then we would have been able to give more comprehensive answers. Until we moved past the `use DBI` error, we had no way of knowing that you didn't have DBD::SQLite installed. It really pays to make your questions as clear as possible. – Dave Cross Jan 22 '19 at 12:12
  • @DaveCross I completely agree on the clarity of question part. I'm not exactly new to StackExchange and been on AskUbuntu and U&L, so I know the clarity of the question is significant. But my experience with Perl is quite limited - I'm mostly a Python and bash user - so probably that contributes to the way the question is worded. – Sergiy Kolodyazhnyy Jan 22 '19 at 12:25