Search code examples
perlmodulerelative-path

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?


Solution

  • 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;