Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 继承和子方法_Perl - Fatal编程技术网

Perl 继承和子方法

Perl 继承和子方法,perl,Perl,由于某些原因,我无法访问边界对象上的子方法。我希望能提供尽可能详细的答案,因为我对perl的继承仍然有点困惑,尤其是bless部分。此外,对于总体设计,任何建设性的批评都是很好的 Generic.pm(基类) 下午六时 package AccessList::Extended; use strict; use warnings; use AccessList::Generic; use base qw(AccessList::Generic); sub new { my ($clas

由于某些原因,我无法访问边界对象上的子方法。我希望能提供尽可能详细的答案,因为我对perl的继承仍然有点困惑,尤其是bless部分。此外,对于总体设计,任何建设性的批评都是很好的

Generic.pm(基类)

下午六时

package AccessList::Extended;
use strict;
use warnings;
use AccessList::Generic;

use base qw(AccessList::Generic);

sub new {
    my ($class, @args) = @_;
    my $self = $class->SUPER::new(@args);
    return $self;
}


1;
下午六时

package AccessList::Extended::Boundary;
use strict;
use warnings;
use AccessList::Extended;

use base qw(AccessList::Extended);

sub new {
    my ($class, @args) = @_;
    my $self = $class->SUPER::new(@args);
    return $self;
}

sub get_acl_information {
    my ($self) = @_;
    return;
}

1;
失败测试

can_ok('AccessList::Extended::Boundary', 'get_acl_information');
错误消息

#   Failed test 'AccessList::Extended::Boundary->can('get_acl_information')'
#   at t/b1.t line 42.
#     AccessList::Extended::Boundary->can('get_acl_information') failed
# Looks like you failed 1 test of 2.

我看不出你的帖子有任何问题。问题肯定出在你没有发布的内容上。您是否忘记加载AccessList::Extended::Boundary

$ find -type f
./AccessList/Extended/Boundary.pm
./AccessList/Extended.pm
./AccessList/Generic.pm

$ perl -E'
   use Test::More tests => 1;
   use AccessList::Extended::Boundary;
   can_ok("AccessList::Extended::Boundary", "get_acl_information");
'
1..1
ok 1 - AccessList::Extended::Boundary->can('get_acl_information')

顺便说一句,没有必要在派生类中编写单独的构造函数,除非它们需要做一些特殊的事情。父类构造函数将被继承。请注意,
base
加载从中继承的模块,因此无需单独加载。感谢您的帮助和建议。非常有用。
$ find -type f
./AccessList/Extended/Boundary.pm
./AccessList/Extended.pm
./AccessList/Generic.pm

$ perl -E'
   use Test::More tests => 1;
   use AccessList::Extended::Boundary;
   can_ok("AccessList::Extended::Boundary", "get_acl_information");
'
1..1
ok 1 - AccessList::Extended::Boundary->can('get_acl_information')