Perl use和require区别

use 发生在编译期间,代码没有运行

use是在当前默认的@INC里面去寻找,一旦模块不在@INC中的话,use是不可以引入的,但是require是可以指定路径

require 是编译时引入


[oracle@dwh1 lib]$ pwd
/oracle/dev/lib
[oracle@dwh1 lib]$ cat Mypack.pm 
use strict;
use warnings;

sub printlog {
 print "test
";
};
1;


[oracle@dwh1 myapp]$ perl a3.pl 
Can't locate Mypack.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at a3.pl line 2.
BEGIN failed--compilation aborted at a3.pl line 2.


[oracle@dwh1 myapp]$ cat a3.pl 
unshift @INC,"/oracle/dev/lib";
require  Mypack;
printlog();

[oracle@dwh1 myapp]$ perl a3.pl 
test

原文地址:https://www.cnblogs.com/hzcya1995/p/13351929.html