在我的例子中,perl中的继承显示了一个错误

在我的例子中,perl中的继承显示了一个错误,perl,inheritance,perl-module,Perl,Inheritance,Perl Module,我有两个perl模块,一个是e.pm,两个是.pm,还有一个是perl脚本 package one; sub foo { print "this is one\n"; } sub goo { print "This is two\n"; } 1; 下午两点 package two; use one; @ISA=(one); sub hoo { print "this is three \n"; } 1; inherit.pl use two; foo(); 当我执行inherit

我有两个perl模块,一个是e.pm,两个是.pm,还有一个是perl脚本

 package one;
sub foo
{
 print "this is one\n";
}
sub goo
{
 print "This is two\n";
}
1;
下午两点

package two;
use one;
@ISA=(one);
sub hoo
{
  print "this is three \n";
}
1;
inherit.pl

use two;
foo();
当我执行inherit.pl时,我得到以下错误

 Undefined subroutine &main::foo called at inherit.pl line 2.

遗产与此无关。您没有导出任何内容,因此
foo
子例程不在脚本的命名空间中

事实上,您还混合了面向对象的概念(继承)和经典的Perl模块。如果您有一个非oop模块,您可以使用exporter并将Sub引入脚本。如果您有一个类,则可以从另一个类继承。但是,您通常不会导出任何内容

现在,如果您想使用
two
one
导入一些东西,基本上构建一些类似于公共函数集合的东西,那么您需要在
one
中使用Exporter,然后在
two
中使用one,然后在
two
中使用Exporter导出从
one
导入的函数


听起来很复杂?是的,因为它很复杂。这样做并没有真正的好处,除非在脚本中保存一行
使用一行

继承对对象有效。您试图做的是导入,而不是继承。我在下面概述了继承和导入的示例

继承:

One.pm

package One;

sub foo {
    print "this is one\n";
}
1;
package Two;

# Note the use of 'use base ...;'. That means that we'll inherit 
# all functions from the package we're calling it on. We can override
# any inherited methods by re-defining them if we need/want

use base 'One';

sub new {
    return bless {}, shift;
}
1;
package One;

use Exporter qw(import);
our @EXPORT_OK = qw(foo); # allow user to import the 'foo' function if desired

sub foo {
    print "this is one\n";
}
1;
package Two;
use One qw(foo); # import foo() from One

use Exporter qw(import);
our @EXPORT_OK = qw(foo); # re-export it so users of Two can import it

1;
Two.pm

package One;

sub foo {
    print "this is one\n";
}
1;
package Two;

# Note the use of 'use base ...;'. That means that we'll inherit 
# all functions from the package we're calling it on. We can override
# any inherited methods by re-defining them if we need/want

use base 'One';

sub new {
    return bless {}, shift;
}
1;
package One;

use Exporter qw(import);
our @EXPORT_OK = qw(foo); # allow user to import the 'foo' function if desired

sub foo {
    print "this is one\n";
}
1;
package Two;
use One qw(foo); # import foo() from One

use Exporter qw(import);
our @EXPORT_OK = qw(foo); # re-export it so users of Two can import it

1;
inherit.pl

use warnings;
use strict;

use Two;

my $obj = Two->new;
$obj->foo;
use warnings;
use strict;

use Two qw(foo);

foo();
进口:

One.pm

package One;

sub foo {
    print "this is one\n";
}
1;
package Two;

# Note the use of 'use base ...;'. That means that we'll inherit 
# all functions from the package we're calling it on. We can override
# any inherited methods by re-defining them if we need/want

use base 'One';

sub new {
    return bless {}, shift;
}
1;
package One;

use Exporter qw(import);
our @EXPORT_OK = qw(foo); # allow user to import the 'foo' function if desired

sub foo {
    print "this is one\n";
}
1;
package Two;
use One qw(foo); # import foo() from One

use Exporter qw(import);
our @EXPORT_OK = qw(foo); # re-export it so users of Two can import it

1;
Two.pm

package One;

sub foo {
    print "this is one\n";
}
1;
package Two;

# Note the use of 'use base ...;'. That means that we'll inherit 
# all functions from the package we're calling it on. We can override
# any inherited methods by re-defining them if we need/want

use base 'One';

sub new {
    return bless {}, shift;
}
1;
package One;

use Exporter qw(import);
our @EXPORT_OK = qw(foo); # allow user to import the 'foo' function if desired

sub foo {
    print "this is one\n";
}
1;
package Two;
use One qw(foo); # import foo() from One

use Exporter qw(import);
our @EXPORT_OK = qw(foo); # re-export it so users of Two can import it

1;
import.pl

use warnings;
use strict;

use Two;

my $obj = Two->new;
$obj->foo;
use warnings;
use strict;

use Two qw(foo);

foo();

请注意,在下一个Perl版本(5.26.0)中,
@INC
默认情况下将不包括当前工作目录,因此要
使用一个
使用两个如果这些模块文件位于本地目录中,则必须添加
use lib'
unshift@INC'或类似内容。

可能重复@dubes我不同意该评估。感谢您解释继承和导入