Does Perl use / require a delayed path?

If I have a file .pm, is there any way that I can have useit without putting it in my path @INC? I think that would be clearer in my particular use case - clearer than using relative paths or adding this directory to @INC.

Edit: Clarification:

I was hoping to avoid having to iterate over each item in @INCand instead indicate directly which file is of interest to me. For example, in Node.JS, it require('something')will search for a list of paths, but it require('/specific/something')will go right where I say it.

In Perl, I'm not sure if this is the same functionality as in require, but it seems to work.

However, instructions userequire simple words. It puzzled me a little how to enter the absolute path.

+5
source share
5 answers

According to the discussion in the comments, I would suggest using require. As below

require "pathto/module/Newmodule.pm";

Newmodule::firstSub();

You can also use other options as shown below.

  • use lib 'pathto/module'; This line should be added to each file in which you want to use the module.

use lib 'pathto / module'; use Newmodule;

  • PERL5LIB. ~/.bashrc, @INC. , PERL5LIB @INC. . apache httpd.conf,

    SetEnv PERL5LIB /fullpath/to/module
    
  • BEGIN.

+4

:

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

@INC (, , use lib):

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

, PERL5LIB var.

export PERL5LIB=/home/ikegami/perl/lib

script, :

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

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
+1

Module:: Load

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

FindBin :: libs does the trick:

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

use FindBin::libs;
0
source

All Articles