+ -
当前位置:首页 → 问答吧 → 为什么 Exporter 模块 不像perldoc 里面说的正常工作?

为什么 Exporter 模块 不像perldoc 里面说的正常工作?

时间:2010-10-26

来源:互联网

http://perldoc.perl.org/Exporter.html

如题

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

我写了个超级简单的 pm 。但是 use的时候无法调入 main:: 里面 。。。为啥呢:
c.pm:
  1. package c;
  2. use Exporter;

  3. @EXPORT = qw/ddiiee/;

  4. sub ddiiee() {
  5.         die "die die die";
  6. }
  7. 1;
复制代码
fork:
  1. #!/bin/usr/perl -w
  2. use strict;
  3. use lib './';
  4. use c;

  5. if(fork()) {
  6.         print "i am parent,juse die see the zom";
  7.         ddiiee();
  8. }

  9. else {
  10.         print "i am child and iam gonna exit";
  11.         sleep 2;
  12.         exit 0 ;
  13. }
复制代码
output:
  1. jerry@hazard:~/Desktop/chinaunix/perl> perl fork
  2. Undefined subroutine &main::ddiiee called at fork line 8.
  3. i am parent, see the zomjerry@hazard:~/Desktop/chinaunix/perl> i am child and iam gonna exit
  4. jerry@hazard:~/Desktop/chinaunix/perl>
复制代码

作者: nuclearxin   发布时间: 2010-10-26

  1. package c;
  2. use Exporter;

  3. our @EXPORT = qw/ddiiee/;
  4. our @ISA = qw/Exporter/;

  5. sub ddiiee
  6. {
  7.         die "die die die";
  8. }

  9. 1;
复制代码

作者: 黑色阳光_cu   发布时间: 2010-10-26

我知道加上  
@ISA = qw/Exporter/;
就没问题
问题是 为什么?

作者: nuclearxin   发布时间: 2010-10-26

回复 黑色阳光_cu


    我知道加上  
@ISA = qw/Exporter/;
就没问题
问题是 为什么?

作者: nuclearxin   发布时间: 2010-10-26

我用的是use 来加在模块 不是用 require 来加在
use的华 应该自动导入 Exporter里面的  import  函数 阿
正如 docperl里面解释的

如果我想用require的话我 应该加入@ISA = qw/Exporter/;

应该是这样阿
看url里面的例子 一个用use 一个用require


  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

作者: nuclearxin   发布时间: 2010-10-26

继承机制,package c继承了Exporter里的import方法。

作者: 黑色阳光_cu   发布时间: 2010-10-26



QUOTE:
继承机制,package c继承了Exporter里的import方法。
黑色阳光_cu 发表于 2010-10-26 11:18




    同样的问题 你说的还是 @ISA;

你的意思是 Exporter 有 的 import   无法用use 带入进来是吗?

看他的例子!
用use的时候根本没有 @ISA

作者: nuclearxin   发布时间: 2010-10-26

本帖最后由 黑色阳光_cu 于 2010-10-26 11:26 编辑


QUOTE:
同样的问题 你说的还是 @ISA;

你的意思是 Exporter 有 的 import   无法用use 带入进来是吗 ...
nuclearxin 发表于 2010-10-26 11:23



给你个最简化的import,只能导入函数~~~
  1. package c;
  2. use strict;
  3. use warnings;

  4. our @EXPORT = qw/ddiiee/;

  5. sub ddiiee
  6. {
  7.         die "die die die";
  8. }

  9. sub import
  10. {
  11.         no strict qw(refs);
  12.         foreach my $s (@EXPORT)
  13.         {
  14.                 if (__PACKAGE__->can($s))
  15.                 {
  16.                         *{"main::$s"} = \&{*{__PACKAGE__ . "::" . $s}};
  17.                 }
  18.         }
  19. }

  20. 1;
复制代码

作者: 黑色阳光_cu   发布时间: 2010-10-26

ok
仔细看了看例子


nnd

nnd

Exporter 的 import   就是无法用use 加进来。

Exporter 的作者为啥不把 import加进来阿?

必须要 手动 加入进来

作者: nuclearxin   发布时间: 2010-10-26



QUOTE:
ok
仔细看了看例子


nnd

nnd

Exporter 的 import   就是无法用use 加进来。

Exporter 的作者 ...
nuclearxin 发表于 2010-10-26 11:26




use一个包时,Perl会去找包里的import方法,当import方法不存在,就会按*ISA{ARRARY}里指明的继承顺序,在一个一个母类里找,直到找到。这也是@ISA要声明为our类型的原因。

作者: 黑色阳光_cu   发布时间: 2010-10-26

貌似成了 鸡生蛋 的问题了
第一个 import必须要手动 那进来对吗?

作者: nuclearxin   发布时间: 2010-10-26

本帖最后由 黑色阳光_cu 于 2010-10-26 12:35 编辑


QUOTE:
貌似成了 鸡生蛋 的问题了
第一个 import必须要手动 那进来对吗?
nuclearxin 发表于 2010-10-26 11:32



抱歉,我以为你问@ISA起什么作用呢?
好像Exporter默认不导出import函数,要写 use Expoter qw(import); 才可以
  1. package c;

  2. use strict;
  3. use warnings;
  4. use Exporter qw(import);

  5. our @EXPORT = qw/ddiiee/;

  6. sub ddiiee
  7. {
  8.         die "die die die";
  9. }

  10. 1;
复制代码

作者: 黑色阳光_cu   发布时间: 2010-10-26