How do I include a Perl module that's in a different directory?

后端 未结 6 2420
天涯浪人
天涯浪人 2020-12-07 16:32

How do I include a Perl module that\'s in a different directory? It needs to be a relative path from the module that\'s including it.

I\'ve tried

p         


        
6条回答
  •  再見小時候
    2020-12-07 17:01

    EDIT: Putting the right solution first, originally from this question. It's the only one that searches relative to the module directory:

    use FindBin;                 # locate this script
    use lib "$FindBin::Bin/..";  # use the parent directory
    use yourlib;
    

    There's many other ways that search for libraries relative to the current directory. You can invoke perl with the -I argument, passing the directory of the other module:

    perl -I.. yourscript.pl
    

    You can include a line near the top of your perl script:

    use lib '..';
    

    You can modify the environment variable PERL5LIB before you run the script:

    export PERL5LIB=$PERL5LIB:..
    

    The push(@INC) strategy can also work, but it has to be wrapped in BEGIN{} to make sure that the push is run before the module search:

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

提交回复
热议问题