【问题标题】:Not able to use the interface module in Perl无法在 Perl 中使用接口模块
【发布时间】:2013-10-08 12:20:44
【问题描述】:

我是 Perl 的初学者。我正在尝试使用CPAN Interface module,但我无法使其工作。我已经按照this page 上的说明安装了模块。 我正在使用 EPIC-Eclipse。我正在尝试实现在同一网站上给出的示例。示例如下: 这是 Bouncable 界面。

  package Bouncable;

  use Class::Interface;
  &interface;   # this actually declares the interface

  sub bounce;
  sub getBounceBack;

  1;

这是实现 Bouncable 接口的 Ball 类。

  package Ball;

  use Class::Interface;
  &implements( 'Bouncable' );

  sub bounce {
    my $self = shift;
    print "The ball is bouncing @ ".$self->getBounceBack." strength"
  }

  sub getBounceBack {
    return 10;
  }

  1;

代码非常简单明了。但是我遇到了以下错误,我无法摆脱它。

Ball tries to implement non existing interface Bouncable -- Interface Bouncable does not use the interface module. at D:/Eclipse projects/PerlTrial/Bouncable.pm line 4.
Compilation failed in require at (eval 3) line 1.
BEGIN failed--compilation aborted at (eval 3) line 1.
 at D:/Eclipse projects/PerlTrial/Ball.pm line 4.

任何帮助表示赞赏!谢谢

【问题讨论】:

  • 你能在 Eclipse 之外运行它吗?同样在命令提示符下,如果您键入 perldoc Class::Interface 会发生什么?
  • 哦,别忘了 use strict; use warnings; 示例将其关闭,但您应该始终使用它
  • 也无法从命令提示符运行它。我犯了同样的错误。当我输入 perldoc Class::Interface 时,我得到了我在上面的问题中提到的文档页面
  • 我查看了模块代码,您需要删除 use Class::interface 行前的空格我可能建议您使用更现代的模块,例如 Moose

标签: perl interface perl-module cpan


【解决方案1】:

模块不喜欢行前的空白

Bouncable.pm

use strict;
package Bouncable;

use Class::Interface;
interface;   # this actually declares the interface

sub bounce;
sub getBounceBack;

1;

球.pm

use strict;
package Ball;

use Class::Interface;
implements( 'Bouncable' );

sub bounce {
    my $self = shift;
    print "The ball is bouncing @ ".$self->getBounceBack." strength"
}

sub getBounceBack {
    return 10;
}

1;

【讨论】:

  • 按照模块代码,上述两个模块中的接口和实现前是不是必须加'&'?
【解决方案2】:

在我看来,这是一个复制粘贴或 Windows 换行问题。在接口模块第 395 行中,替换只删除空格而不是空格。提到的错误消息立即出现。

$line =~ s/\ +$//;

应该替换为

$line =~ s/\s+$//;

这只是我的最佳猜测,无法在这里查看。如果这不起作用,请联系该模块的维护者。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-07
    • 2011-06-15
    • 1970-01-01
    • 2013-05-19
    相关资源
    最近更新 更多