2019独角兽企业重金招聘Python工程师标准>>>

Exporter Module

  1. usage                                                                                                                                             implements an import method which allows a module to export functions and variables to its users' namespaces.
package YourModule;
require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(munge frobnicate);  # symbols to export on request
package YourModule;
use Exporter 'import'; # gives you Exporter's import() method directly
@EXPORT_OK = qw(munge frobnicate);  # symbols to export on request

Perl automatically calls the import method when processing a use statement for a module.

use YourModule qw(frobnicate);      # import listed symbols
frobnicate ($left, $right)          # calls YourModule::frobnicate

@ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS

 @ISA:Within each package a special array called @ISA tells Perl where else to look for a method if it can't find the method in that package. This is how Perl implements inheritance.

Each element of the @ISA array is just the name of another package that happens to be used as a class. The packages are recursively searched (depth first) for missing methods, in the order that packages are mentioned in @ISA.

This means that if you have two different packages (say, Mom and Dad) in a class's @ISA, Perl would first look for missing methods in Mom and all of her ancestor classes before going on to search through Dad and his ancestors. Classes accessible through @ISA are known as base classes of the current class, which is itself called the derived class.

@EXPORT contains list of symbols (subroutines and variables) of the module to be exported into the caller namespace.

@EXPORT = qw(...); 
Symbols to export by default

@EXPORT_OK this array stores the subroutins to be exported only on request.
@EXPORT_OK = qw(...); 
Symbols to export on request.

%EXPORT_TAGS = (tag => [...]); 
Define names for sets of symbols
Since the symbols listed within %EXPORT_TAGS must also appear in either @EXPORT or EXPORT_OK,
two utility functions are provided that allow you to easily add tagged sets of symbols to @EXPORT or @EXPORT_OK:
%EXPORT_TAGS = (Bactrian => [qw(aa bb cc)], Dromedary => [qw(aa cc dd)]);
Exporter::export_tags('Bactrian'); # add aa, bb and cc to @EXPORT
Exporter::export_ok_tags('Dromedary'); # add aa, cc and dd to @EXPORT_OK

How to EXPORT

The arrays  @EXPORT  and  @EXPORT_OK   in a module hold lists of symbols that are going to be exported into the users name space by default, or which they can request to be exported, respectively. The symbols can represent functions, scalars, arrays, hashes, or typeglobs.

How to import

In other files which wish to use your module there are three basic ways for them to load your module and import its symbols:

  1. use YourModule;                                                                                                                                            This imports all the symbols from YourModule's @EXPORT into the namespace of the use statement.
  2. use YourModule ();                                                                                                                                   This causes perl to load your module but does not import any symbols.
  3. use YourModule qw(...);                                                                                                                             This imports only the symbols listed by the caller into their namespace. All listed symbols must be in your @EXPORT or @EXPORT_OK, else an error occurs. The advanced export features of Exporter are accessed like this, but with list entries that are syntactically distinct from symbol names.

@INC

@INC is a special Perl variable which is the equivalent of the shell's PATH variable. Whereas PATH contains a list of directories to search for executables, @INC contains a list of directories from which Perl modules and libraries can be loaded.

When you use(), require() or do() a filename or a module, Perl gets a list of directories from the @INC variable and searches them for the file it was requested to load. If the file that you want to load is not located in one of the listed directories, you have to tell Perl where to find the file. You can either provide a path relative to one of the directories in @INC, or you can provide the full path to the file.

%INC

%INC is another special Perl variable that is used to cache the names of the files and the modules that were successfully loaded and compiled by use(), require() or do() statements.Before attempting to load a file or a module with use() or require(), Perl checks whether it's already in the %INC hash. If it's there, the loading and therefore the compilation are not performed at all. Otherwise the file is loaded into memory and an attempt is made to compile it.do() does unconditional loading--no lookup in the %INC hash is made.

If the file is successfully loaded and compiled, a new key-value pair is added to %INC. The key is the name of the file or module as it was passed to the one of the three functions we have just mentioned, and if it was found in any of the @INC directories except "." the value is the full path to it in the file system.

% perl -e 'print join "\n", @INC'/usr/lib/perl5/5.00503/i386-linux/usr/lib/perl5/5.00503/usr/lib/perl5/site_perl/5.005/i386-linux/usr/lib/perl5/site_perl/5.005.

Notice the . (current directory) is the last directory in the list.

% perl -e 'use strict; print map {"$_ => $INC{$_}\n"} keys %INC'strict.pm => /usr/lib/perl5/5.00503/strict.pm

Now let's create the simplest module in /tmp/test.pm:

test.pm-------1;

It does nothing, but returns a true value when loaded. Now let's load it in different ways:

% cd /tmp% perl -e 'use test; print map {"$_ => $INC{$_}\n"} keys %INC'test.pm => test.pm

Since the file was found relative to . (the current directory), the relative path is inserted as the value. If we alter @INC, by adding /tmp to the end:

% cd /tmp% perl -e 'BEGIN{push @INC, "/tmp"} use test; \print map {"$_ => $INC{$_}\n"} keys %INC'test.pm => test.pm

require

require() reads a file containing Perl code and compiles it.Before attempting to load the file it looks up the argument in %INC to see whether it has already been loaded. If it has, require() just returns without doing a thing. Otherwise an attempt will be made to load and compile the file.

This differs from use in that included files effectively become additional text for the current script. Functions, variables, and other objects are not imported into the current name space, so if the specified file includes a package definition, then objects will require fully qualified names.

The specified module is searched for in the directories defined in @INC, looking for a file with the specified name and an extension of .pm.

1、require用于载入module或perl程序(.pm后缀可以省略,但.pl必须有)
2、require在运行时载入(验证)module

require htolib::test
这条语句相当于:require “htolib/test.pm”;

如果使用require 'filename'或者require "filename"来包含文件的话,使用方法和do完全近似;

  1. 如果不加单引号或者双引号的话,后面的Module将被解析为Perl的模块即.pm文件,然后根据@INC Array中搜索Module.pm文件。首先在当前目录下搜索Module.pm的文件(用户自定义的),如果找不到再去Perl的 (@INC contains: C:/Perl/site/lib C:/Perl/lib .)寻找。

use

use(), just like require(), loads and compiles files containing Perl code, but it works with modules only and is executed at compile time.

The only way to pass a module to load is by its module name and not its filename. If the module is located in MyCode.pm, the correct way to use() it is:

use MyCode

and not:

use "MyCode.pm"

use() translates the passed argument into a file name replacing :: with the operating system's path separator (normally /) and appending .pm at the end. So My::Module becomes My/Module.pm.

use() is exactly equivalent to:

BEGIN { require Module; Module->import(LIST); }

Internally it calls require() to do the loading and compilation chores. When require() finishes its job, import() is called unless () is the second argument. The following pairs are equivalent:

use MyModule;BEGIN {require MyModule; MyModule->import; }use MyModule qw(foo bar);BEGIN {require MyModule; MyModule->import("foo","bar"); }use MyModule ();BEGIN {require MyModule; }

do

While do() behaves almost identically to require(), it reloads the file unconditionally. It doesn't check %INC to see whether the file was already loaded.

If do() cannot read the file, it returns undef and sets $! to report the error. If do() can read the file but cannot compile it, it returns undef and puts an error message in $@. If the file is successfully compiled, do() returns the value of the last expression evaluated.

转载于:https://my.oschina.net/u/347414/blog/267747

Perl中use、require的用法和区别相关推荐

  1. php require的用法,php7中include 和 require 语句用法和区别

    include (或 require)语句会获取指定文件中存在的所有文本/代码/标记,并复制到使用 include 语句的文件中. 包含文件很有用,如果您需要在网站的多张页面上引用相同的 PHP.HT ...

  2. php中的foreach和js中的foreach的用法和区别

    PHP中的foreach循环: 主要用于遍历数组 例如: (1)// $colors=array("red","yellow","blue" ...

  3. Python中numpy中tile和repeat用法和区别

    tile tile(数组,重复次数) tile(数组,(第1维重复次数,第2维重复次数,-,第n维重复次数)) 例子 tile(数组,(沿着第1维的轴重复次数,沿着第2维的轴重复次数,-,沿着第n维的 ...

  4. perl中q,qq,qw,qr的区别。

    q运算符对'号转义的方式 $someword = 'i \'ve some money'; 可以等价于: $someword = q~i 've some money~; qq运算符对"号转 ...

  5. jsp 中forward 和 Redirect 的用法区别

    jsp中跳转的用法和区别 区别 用法 forward的用法 redirect的用法 区别 首先我们要知道的是forward是转发,地址用的是原地址,但是内容变为设置转向地址的内容. redirect ...

  6. Perl中use和require用法对比

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

  7. php中include和require,在PHP中include和require到底有什么区别呢?

    在PHP中include和require到底有什么区别呢?看这里的例子就知道了 include.php3的运行结果是: 这是inc1.inc文件中的一个变量的值! 这是inc2.inc文件中的一个变量 ...

  8. PHP中MySQL、MySQLi和PDO的用法和区别

    MySQL 是 PHP 操作 MySQL 数据库最原始的 Extension.MySQLi 的 i 代表 Improvement ,提供了相对进阶的功能,就 Extension 而言,本身也增加了安全 ...

  9. Java-线程中sleep()、wait()和notify()和notifyAll()、suspend和resume()、yield()、join()、interrupt()的用法和区别

    Java线程中sleep().wait()和notify()和notifyAll().suspend和resume().yield().join().interrupt()的用法和区别 从操作系统的角 ...

最新文章

  1. 用于小型图形挖掘研究的瑞士军刀:空手道俱乐部的图表学习Python库
  2. Oracle 11g Win10卸载 ,亲测
  3. JZOJ 5377. 【NOIP2017提高A组模拟9.19】开拓
  4. springboot整合shiro和session的详细过程和自定义登录拦截器
  5. DCMTK:DSRNumericMeasurementValue类的测试程序
  6. CodeForces - 1321B Journey Planning(思维)
  7. Crazy Diamond CodeForces - 1148C(思维构造)
  8. .Net Core with 微服务 - Seq 日志聚合
  9. Java 多线程 —— 常用并发容器
  10. Jmeter中JDBC Connection Configuration实现MySQL JDBC Request数据库处理
  11. 【TensorFlow系列二】经典损失函数(交叉熵、均方差)
  12. 采集gpu_GPU温度的采集
  13. ActionMapping
  14. AOJ0121 Seven Puzzle【BFS】
  15. 锂离子电池容量计量之库仑计法
  16. 算法学习笔记(5) 传递闭包
  17. java点击登录实现跳转_页面跳转的简单实现(单点登录)
  18. 空间信息产业的八大极客技术
  19. 跑深度模型的显卡_不止显卡!这些硬件因素也影响着你的深度学习模型性能
  20. springboot利用官方SDK(wechatpay-apache-httpclient)接入微信支付V3

热门文章

  1. python or妙用
  2. python 知识点记录二
  3. linux+nm+内容详解,【Linux】nm命令中符号类型详解
  4. java课程设计 成绩_Java课程设计—学生成绩管理系统(201521123004-林艺如)
  5. Python 十六进制转Base64_python基础day03笔记
  6. discuz admin.php无法登录,discuz搬家管理员无法登录后台解决方法
  7. Leaflet中加载离线OSM瓦片地图(使用OfflineMapMaker切割下载离线png地图文件)
  8. Leaflet中原生方式实现测量面积
  9. Oracle11g服务详细介绍及哪些服务是必须开启的?
  10. Windows上配置SSHKey到GItHub