EXPORT 和EXPORT_OK

来源:互联网 发布:浙江中控怎么样知乎 编辑:程序博客网 时间:2024/06/08 12:53
EXPORT 实现模块的默认导入方法In module YourModule.pm:  [root@node01 perl]# cat ex1.pm   package ex1;  require Exporter;  @ISA = qw(Exporter);  @EXPORT_OK = qw($munge frobnicate);  # symbols to export on request  my $munge='aaaa';1;[root@node01 perl]# cat a3.pl use ex1 qw($munge);;print $munge;[root@node01 perl]# perl a3.pl [root@node01 perl]# [root@node01 perl]# cat ex1.pm   package ex1;  require Exporter;  @ISA = qw(Exporter);  @EXPORT_OK = qw($munge frobnicate);  # symbols to export on request  $munge='aaaa';1;[root@node01 perl]# cat a3.pl use ex1 qw($munge);;print $munge;[root@node01 perl]# perl a3.pl aaaa[root@node01 perl]# 包里的变量导出 需要全局变量[root@node01 perl]# cat ex1.pm   package ex1;  require Exporter;  @ISA = qw(Exporter);  @EXPORT_OK = qw($munge frobnicate);  # symbols to export on request  $munge='aaaa';  $a='bbbbbbbb';  $c='cccccccc';  sub frobnicate {   my $a=shift;   my $b=shift;   return $a + $b;};1;[root@node01 perl]# cat a3.pl use ex1 qw($munge frobnicate);;print $munge;print "\n";print frobnicate(23,34);print "\n";print $a;print "\n";[root@node01 perl]# perl a3.pl aaaa57描述:Exporter 模块实现一个导入方法允许一个模块来导出函数和变量到用户的名字空间。很多模块使用Exporter 相比实现它们自己的import 方法。因为Exporter 提供一个高度灵活的接口.Perl 自动 调用import 方法 当处理一个use 语句用于一个模块。模块和使用是记录在 perlfunc and perlmod.了解模块的内容和如何使用语句操作是重要的对于了解Exporter.如何导出:数组 @EXPORT 和@EXPORT_OK 是一个模块用于一组符号表准备被导出到用户的名字空间,或者 它们可以被请求导出,分别的。符号可以表示函数,标量,数组,hashes或者类型团。符号必须给定全名 但是函数前面的符号是可选的    @EXPORT    = qw(afunc $scalar @array);   # afunc is a function    @EXPORT_OK = qw(&bfunc %hash *typeglob); # explicit prefix on &bfunc如果你只是导出函数名字 是推荐省略符号,这样更快选择需要导出的不到导出方法名字!不要导出任何否则默认Exports  污染 模块用户的名字空间。如果你必须导出尝试使用@EXPORT_OK优先于@EXPORT 避免冲出通常 任何不被导出的仍旧可以访问从外部 使用YourModule::item_name (or $blessed_ref->method) syntax事实上,它是可以得到私有函数通过:如何导入:在其他文件 希望使用你的模块 有3种基本方式用于加载你的模块和导入它的符号:use YourModule;这个导入所有的符号从YourModule's @EXPORT 到你的名字空间use YourModule ();这个导致Perl来加载你的模块 但是不导入任何符号表use YourModule qw(...);这个将调用列出的符号到名字空间。所有列出的符号必须是在你的@EXPORT or @EXPORT_OK,否则一个错误发生。

原创粉丝点击