Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I "use" a Perl module in a directory not in @INC?

I have a module in the parent directory of my script and I would like to 'use' it.

If I do

use '../Foo.pm'; 

I get syntax errors.

I tried to do:

push @INC, '..'; use EPMS; 

and .. apparently doesn't show up in @INC

I'm going crazy! What's wrong here?

like image 785
Frew Schmidt Avatar asked Oct 08 '08 22:10

Frew Schmidt


People also ask

How do I find out where a Perl module is installed?

instmodsh command provides an interactive shell type interface to query details of locally installed Perl modules. It is a little interface to ExtUtils::Installed to examine locally* installed modules, validate your packlists and even create a tarball from an installed module.


2 Answers

use takes place at compile-time, so this would work:

BEGIN {push @INC, '..'} use EPMS; 

But the better solution is to use lib, which is a nicer way of writing the above:

use lib '..'; use EPMS; 

In case you are running from a different directory, though, the use of FindBin is recommended:

use FindBin;                     # locate this script use lib "$FindBin::RealBin/..";  # use the parent directory use EPMS; 
like image 52
ephemient Avatar answered Oct 09 '22 11:10

ephemient


There are several ways you can modify @INC.

  • set PERL5LIB, as documented in perlrun

  • use the -I switch on the command line, also documented in perlrun. You can also apply this automatically with PERL5OPT, but just use PERL5LIB if you are going to do that.

  • use lib inside your program, although this is fragile since another person on a different machine might have it in a different directory.

  • Manually modify @INC, making sure you do that at compile time if you want to pull in a module with use. That's too much work though.

  • require the filename directly. While this is possible, it doesn't allow that filename to load files in the same directory. This would definitely raise eyebrows in a code review.

like image 23
brian d foy Avatar answered Oct 09 '22 12:10

brian d foy