2

OS: Mac 10.12.1, mysql 5.7.15

perl -e 'print $];'
5.016000

Error message:

Can't locate DBD/mysql.pm in @INC (you may need to install the DBD::mysql module) (@INC contains: /Library/Perl/5.18/darwin-thread-multi-2level /Library/Perl/5.18 /Network/Library/Perl/5.18/darwin-thread-multi-2level /Network/Library/Perl/5.18 /Library/Perl/Updates/5.18.2 /System/Library/Perl/5.18/darwin-thread-multi-2level /System/Library/Perl/5.18 /System/Library/Perl/Extras/5.18/darwin-thread-multi-2level /System/Library/Perl/Extras/5.18 .) at ./testDBConnection.pl line 8. BEGIN failed--compilation aborted at ./testDBConnection.pl line 8.

Source Code:

#!/usr/bin/perl

#use lib '/usr/bin/cpan'; 
use strict; 
use FileHandle; 
use File::Spec; 
use DBI; 
use DBD::mysql;

When I run: cpan DBD::mysql

Reading '/Users/user/.cpan/Metadata'
  Database was generated on Fri, 18 Nov 2016 19:41:02 GMT
DBD::mysql is up to date (4.039).

When I run: perldoc -lm DBI DBD::mysql

/Users/user/perl5/perlbrew/perls/perl-5.16.0/lib/site_perl/5.16.0/darwin-2level/DBI.pm

/Users/user/perl5/perlbrew/perls/perl-5.16.0/lib/site_perl/5.16.0/darwin-2level/DBD/mysql.pm

Any suggestions?

Sinan Ünür
  • 113,391
  • 15
  • 187
  • 326
user1256378
  • 663
  • 2
  • 12
  • 29
  • Looks like you are using perlbrew. Check out https://perlbrew.pl/ – Red Cricket Nov 19 '16 at 19:25
  • try running your script with perl directly: `perl script.pl`. Also, changing your shebang line to `#!/usr/bin/env perl` may fix it as well – stevieb Nov 19 '16 at 19:46
  • @stevieb The problem is that the OP is telling the shell to use `/usr/bin/perl` to execute the script. The script, the shell, and the environment are all under the OP's control. There is no need for workarounds. – Sinan Ünür Nov 19 '16 at 19:49

2 Answers2

3

Your shebang line refers to /usr/bin/perl. That is the interpreter that will be used to execute your script.

/usr/bin/perl is the perl that comes with your system. You did the right thing by not messing with the modules that came with it and instead installing your own. Now, you should tell your script to use it.

It looks like the correct perl is already in your path. Use which perl to see the full path to it.

As a side note, there is really no reason for the

use DBD::mysql;

line. DBI will chose which driver to load based on your connection string.

For more on shebang lines, see my answer on a different question.

Make links to per-version tools by brian d foy may help. I personally find tools such as perlbrew to be more a hindrance than help.

Sinan Ünür
  • 113,391
  • 15
  • 187
  • 326
0

I think you've answered your own question. perldoc tells you that DBI:mysql is installed in /Users/user/perl5/perlbrew/perls/perl-5.16.0/lib/site_perl/5.16.0/darwin-2level/DBD/mysql.pm, and perl tells you that INC does not contain that path.

The simplest solution to start is to either run perl -I <path above> or use lib <path above>;. I'm sure there's a better solution, but this is enough to get you started solving your own problem.

Mort
  • 2,888
  • 1
  • 19
  • 33