zdd
zdd

Reputation: 8757

[perl]How to force perl use modules in my own path?

I want to let perl use the DBI module in my own path(suppose, /home/users/zdd/perl5/lib/DBI), but the sysem also has a DBI module which is /usr/lib/perl5/lib/DBI. when I write the following code in my script, perl use the system path be default, how to force it use the one under my path?

use lib './perl5/lib/DBI';
use DBI;

sub test {
....
}

/usr/lib/perl5/lib/DBI was added to the PATH environment variable in my bash profile, it was used by many scripts, so I can't disable it.

Upvotes: 2

Views: 2656

Answers (2)

imran
imran

Reputation: 1560

In addition to what dan1111 suggested, I would also recommend you print out @INC (just before your use DBI statement) and dump %INC (just after your use DBI statement) to see what your script is doing. That may help you debug the issue.

Upvotes: 1

user1919238
user1919238

Reputation:

The file for the main DBI module is in ./perl5/lib. So your path is not pointing to it.

The DBI folder contains sub-modules of DBI, e.g. DBI::Foo (the :: in module names is a representation of your module directory structure).

Try using ./perl5/lib as your library instead.

Also, using a relative path will fail if the current directory is not what you think it is. If you are in doubt, have your script call cwd to see what the current directory is.

For debugging purposes, it may be helpful to use:

no lib '[main Perl module library path here]';

That way you can be sure you are only using your custom module path. Any failure to find a module will cause an error, rather than silently using the system version.

Update: For more information, see Perldoc on use lib. Perl will use the library that you have specified first. If it does not, that indicates it is not actually finding the module in the location you have given.

Upvotes: 6

Related Questions