user1256378 user1256378 - 21 days ago 6
Perl Question

Why can't my Perl script locate DBD/mysql.pm even though cpan tells me DBD::mysql is installed?

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?

Answer

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.