In Perl, what is the difference between `use lib` and` lib-> import`?

In Perl 5.24.1 what is the difference between using use liband lib->import? I see that both are used to add a directory to @INC. I am using Perl stock without add-ons. I read http://perldoc.perl.org/lib.html and I am not very good at all ways to use it.

Here is an example:

In the following code example, the first use libalways works, while the second lib->importdoes not work in my code. I see that it works in different code on the same machine.

#!/usr/bin/perl -T

use warnings;
use diagnostics;
use strict;
# new perl 2.24.1 requires FindBin
use FindBin;
print "found: $FindBin::Bin\n";
#This always works:
#use lib $FindBin::Bin;
#why does this not always work?
#lib->import($FindBin::Bin);

foreach my $var(@INC){
    print "$var \n";
}
+4
source share
2 answers

use lib EXPRloads lib.pm, and at compile time calls lib->import(EXPR).

lib->import(EXPR) , , lib.pm , . ( import() unimport() , .) , BEGIN, , @INC , use.

+3
use lib $FindBin::Bin;

():

BEGIN { require "lib.pm"; lib->import($FindBin::Bin); }

/ lib.pm ( , BEGIN).

lib->import(...) , , lib , "", . lib , require ( eval "use ..."). , , , , , .

+3

Source: https://habr.com/ru/post/1669773/


All Articles