Exporter 的理解

1685阅读 0评论2011-03-26 zzy7186
分类:LINUX

SYNOPSIS
       In module YourModule.pm:

         package YourModule;
         require Exporter;
         @ISA = qw(Exporter);
         @EXPORT_OK = qw(munge frobnicate);  # symbols to export on request

       or

         package YourModule;
         use Exporter 'import'; # gives you Exporter's import() method directly
         @EXPORT_OK = qw(munge frobnicate);  # symbols to export on request

       In other files which wish to use "YourModule":

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

       Take a look at "Good Practices" for some variants you will like to use
       in modern Perl code.


其他模块是通过Exporter 模块提供的import  方法来将自己的符号导出到current package 的 
当use Module;被执行时perl 会自动调用import method 导入相关符号
use Module;
将@EXPORT 中的所有符号的导入
相当于
 BEGIN { require Module; Module->import( LIST ); }
use Module qw(list);
导入list指定的符号,但这些必须在@EXPORT或@EXPORT_OK数组中的,不然会出错
use Module();
加载模块但是不导入符号相当于
BEGIN { require Module }

如果use module; 后面没加符号list的话 它将导出@EXPORT 数组中的符号。如果加了话 他将导入list指定的符号




上一篇:Vim 的学习笔记 Ⅰ
下一篇:【转】浅谈typeglob