Perl use/require abolute path?

情到浓时终转凉″ 提交于 2019-12-05 07:55:25

As per discussion in comments, I would suggest using require itself. Like below,

require "pathto/module/Newmodule.pm";

Newmodule::firstSub();

Also you can use other options as below

  • use lib 'pathto/module'; This line needs to be added to every file you want to use the module in.

use lib 'pathto/module';
use Newmodule;

  • using PERL5LIB environment variable. Set this on command line using export or add this to ~/.bashrc so that with every login it will be added to your @INC. Remember PERL5LIB adds directory before all @INC directories. So it will be used first. Also you can set it in apache httpd.conf using

    SetEnv PERL5LIB /fullpath/to/module
    
  • Or set it in BEGIN block.

You can use this :

use lib '/path/to/Perl_module_dir'; # can be both relative or absolute
use my_own_lib;

You can modify @INC by yourself (temporarily, no fear, that's what use lib does too) :

BEGIN{ @INC = ( '/path/to/Perl_module_dir', @INC ); } # relative or absolute too
use my_own_lib;

Generally speaking, set the PERL5LIB environment var.

export PERL5LIB=/home/ikegami/perl/lib

If the module to find is intended to be installed in a directory relative to the script, use the following:

use FindBin qw( $RealBin );
use lib $RealBin;
  # or
use lib "$RealBin/lib";
  # or
use lib "$RealBin/../lib";

This will correctly handle symbolic links to the script.

$ mkdir t

$ cat >t/a.pl
use FindBin qw( $RealBin );
use lib $RealBin;
use Module;

$ cat >t/Module.pm
package Module;
print "Module loaded\n";
1;

$ ln -s t/a.pl

$ perl a.pl
Module loaded

You can use the Module::Load module

use Module::Load;
load 'path/to/module.pm';

FindBin::libs does the trick:

# search up $FindBin::Bin looking for ./lib directories
# and "use lib" them.

use FindBin::libs;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!