来源:

http://www.cnblogs.com/itech/archive/2010/11/22/1884345.html

相同:

都可以用来引用module(.PM)。

不同:

1) 区别在于USE是在当前默认的@INC里面去寻找,一旦模块不在@INC中的话,用USE是不可以引入的,但是require可以指定路径;

2)use引入的名称不需要后缀名,默认找.PM文件,而require需要后缀名,且后缀名可以为.PM或.PL;

实例:c:\perl目录下3个文件如下(且module文件的最后必须有1;)

 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->#file : CallModule.pl
use strict; BEGIN {
my $scriptDir = $;
$scriptDir =~ s:\\:/:g;
$scriptDir =~ s,/?[^/]+$,,;
$scriptDir = "." if ($scriptDir eq "");
unshift(@INC, $scriptDir);
} #require "c:\\perl\\pmodule1.pl";
require "pmodule1.pl";
use pmodule2; Hello1();
Hello2(); print "I am caller!\n"; exit ; #file : pmodule1.pl
use strict; sub Hello1
{
print "Hello1\n";
} ; #file : pmodule2.pm
use strict; sub Hello2
{
print "Hello2\n";
} ;

3)use语句是编译时引入的,require是运行时引入的; 下面的例子是错误的:
use Config;
if( $Config{'osname'} eq "MSWin32" ) 

    use Win32Module; 

else

    use UnixModule; 
}
正因为use是发生在编译期,代码没有执行,所以$Config变量的值是无法判断的。在内部,use其实调用了require和import静态函数。import()函数告诉包哪些特征将要被导入当前包中,这意味着用之前不必验证函数或者变量是否合法。而require是不会调用import()的。

4)use引入模块的同时,也引入了模块的子模块。而require则不能引入,要在重新声明;例如

use my

my中包含了一个Module.pm

而require 则需要引入

require my

require my::Module.pm

如果不想让use引入Module.pm则可以用 use require() 加一对括号来说明即可。

5)USE引用模块时,如果模块名称中包含::双冒号,该双冒号将作为路径分隔符,相当于Unix下的/或者Windows下的\。 如:
use MyDirectory::MyModule;
编译器将从@INC指定的目录下的MyDirectory子目录去寻找MyModule模块, 类似
如下路径:
C:\Perl\lib\MyDirectory
C:\Perl\ext\lib\MyDirectory
C:Perl\site\lib\MyDirectory

perl的USE和require的更多相关文章

  1. perl 判断数组相等的三种方法

    1.数组相等,数组成员相同,位置也相同 一般的如果判断@array1 等于 @array2 a.数组长度相同 $#array1=$#array2, 比较数组长度,不能使用length函数,length ...

  2. SWIG 3 中文手册——4. 脚本语言

    目录 4 脚本语言 4.1 两种语言的概览 4.2 脚本语言如何调用 C? 4.2.1 包装器函数 4.2.2 变量链接 4.2.3 常量 4.2.4 结构体与类 4.2.5 代理类 4.3 构建脚本 ...

  3. markdown-it + highlight.js简易实现

    markdown-it 官方demo markdown-it 文档 1.配置highlightjs,针对markdown中各种语言高亮,针对对应的标签 pre code 里面的样式 -- index. ...

  4. Cracking Digital VLSI Verification Interview 第三章

    目录 Programming Basics Basic Programming Concepts Object Oriented Programming Concepts UNIX/Linux Pro ...

  5. Perl 之 use(), require(), do(), %INC and @INC

    来源: http://www.cnblogs.com/itech/archive/2013/03/12/2956185.html 转自:http://perl.apache.org/docs/gene ...

  6. perl学习之:use & require

    相同: 都可以用来引用module(.PM). 不同: 1) 区别在于USE是在当前默认的@INC里面去寻找,一旦模块不在@INC中的话,用USE是不可以引入的,但是require可以指定路径: 2) ...

  7. perl学习之:use and require

    本文和大家重点学习一下Perl use和require用法对比,这两个函数都是一个意思,加载和引用Perl的模块,或者是子程序,区别在于Perl use是在当前默认的里面去寻找,一旦模块不在指定的区域 ...

  8. Perl 的面向对象编程

    转自 http://net.pku.edu.cn/~yhf/tutorial/perl/perl_13.html 拓展阅读 http://bbs.chinaunix.net/forum.php?mod ...

  9. PCRE Perl Compatible Regular Expressions Learning

    catalog . PCRE Introduction . pcre2api . pcre2jit . PCRE Programing 1. PCRE Introduction The PCRE li ...

随机推荐

  1. visual SVN 反编译破解

    今天发现visual SVN 过期了.网上搜索了一下,发现了下面的贴子. http://www.heiqu.com/show-71200-1.html 一看是用.Net写的,大喜,破解就太简单了.本来 ...

  2. 第一章 flume架构介绍

    1.flume概念介绍 1.1 常见的分布式日志收集系统                             Scribe是facebook开源的日志收集系统,在facebook内部已经得到大量的 ...

  3. HDU2363 最短路+贪心

    Cycling Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  4. POJ 2240 Arbitrage(SPFA+邻接矩阵)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cmath> #include<algori ...

  5. deepin2014.1快捷键

    初试deepin2014.1,发现windows很多快捷键在deepin中也完美支持,举例如下: ctrl+shift+n : 新建文件夹 窗口键+E:打开文件系统 窗口键+TAB:3D切换桌面 al ...

  6. shp文件显示

    开发环境 Win7, VS2010 Sp1 QGIS 2.01 #include <qgsapplication.h> #include <qgsproviderregistry.h ...

  7. 一步一步学EF系列1【Fluent API的方式来处理实体与数据表之间的映射关系】

    EF里面的默认配置有两个方法,一个是用Data Annotations(在命名空间System.ComponentModel.DataAnnotations;),直接作用于类的属性上面,还有一个就是F ...

  8. php缓存总结

    php缓存技术: 1.全页面静态化缓存;2.页面部分缓存;3.数据缓存;4.查询缓存;5.按内容变更进行缓;6.内存式缓存;7.apache缓存模块;8.php APC缓存扩展;9.Opcode缓存. ...

  9. LeetCode OJ 229. Majority Element II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  10. LeetCode OJ 62. Unique Paths

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...