Using Class::Interface



我正在使用Class::Interface模块,我正在尝试声明一个稍后将由某些类实现的接口

但是我收到此错误

IPerformable 不是有效的接口。 开始; 在/ProjectDir/IPerformable.pm 第 4 行有一个实现。

这是接口的代码:

package Controllers::IPerformable;
use Class::Interface; 
&interface;
sub start;
1;

下面是一个implements接口的示例类:

package Controllers::PerformTask;
use strict; 
use warnings;
use Carp;
use Exporter 'import';
use Controllers::IPerformable;
use Class::Interface;
implements('Controllers::IPerformable');
sub new {
    # code of the contructor
}
sub start {
    # implementation of the interface
}

这是主程序的一部分:

#!/usr/bin/perl
use strict;
use warnings;
use lib '/ProjectDir/Controllers';
use Controllers::PerformTask;
my $search = PerformTask->new($param1, $param2);
my $taskResult = $search->start();
print "Result of performing the task: $taskResult n";

你在Windows上运行这个吗?此错误报告似乎很合适。

看起来类::接口未维护。自 2008 年以来一直没有新版本,也没有对该错误报告做出任何回应。我会非常谨慎地使用它。

现代OO Perl倾向于使用角色而不是接口。您可能想改为查看驼鹿角色。

这是使用 Moo 和 Moo::Role 完成所需操作的最小方法。你也可以对驼鹿做同样的事情。

IPerformable.pm

package IPerformable;
use strict;
use warnings;
use Moo::Role;
requires 'start';
__PACKAGE__;
__END__

PerformTask.pm

package PerformTask;
use strict;
use warnings;
use Moo;
with 'IPerformable';
has param1 => (is => 'ro', required => 1);
has param2 => (is => 'ro', required => 1);
sub start {
    print "Implements IPerformable interfacen";
}
__PACKAGE__;
__END__

runner.pl

#!/usr/bin/env perl
use strict;
use warnings;
use lib '.';
use PerformTask;
my $search = PerformTask->new({param1 => 'param1', param2 => 'param2'});
my $taskResult = $search->start;
print "Result of performing the task: $taskResult n";

最新更新