我的Dancer应用程序模块中有以下代码:
package Deadlands;
use Dancer ':syntax';
use Dice;
our $VERSION = '0.1';
get '/' => sub {
my ($dieQty, $dieType);
$dieQty = param('dieQty');
$dieType = param('dieType');
if (defined $dieQty && defined $dieType) {
return Dice->new(dieType => $dieType, dieQty => $dieQty)->getStandardResult();
}
template 'index';
};
true;我有一个名为Dice.pm的Moops类,如果我用一个.pl文件测试它,它就能正常工作,但是当我试图通过web浏览器访问它时,我会得到以下错误:无法通过包"Dice“找到对象方法"new”(可能您忘了加载"Dice"?)。
我能用丹瑟做这个吗?
下面是来自Dice.pm的相关代码:
use 5.14.3;
use Moops;
class Dice 1.0 {
has dieType => (is => 'rw', isa => Int, required => 1);
has dieQty => (is => 'rw', isa => Int, required => 1);
has finalResult => (is => 'rw', isa => Int, required => 0);
method getStandardResult() {
$self->finalResult(int(rand($self->dieType()) + 1));
return $self->finalResult();
}
}发布于 2013-12-11 22:19:11
我本来想说您忘记了package Dice在Dice.pm中,但是在阅读了Moops之后,我对名称空间感到困惑。
让我们来看看Moops文件。
如果在main以外的包中使用Moops,则声明中使用的包名由该外部包“限定”,除非它们包含"::“。例如: 包Quux;使用Moops;类Foo {}#声明Quux::Foo类Xyzzy::Foo #声明Xyzzy::Foo扩展Foo {}#.扩展Quux::Foo类::Baz {}#声明Baz
如果class Dice在Dice.pm中,如果我正确阅读,它实际上将变成Dice::Dice。因此,您必须使用use Dice并使用Dice::Dice->new创建对象。
为了使包Dice在Dice.pm中使用Moops,我相信您需要像这样声明这个类:
class ::Dice 1.0 {
# ^------------- there are two colons here!
has dieType => (is => 'rw', isa => Int, required => 1);
has dieQty => (is => 'rw', isa => Int, required => 1);
has finalResult => (is => 'rw', isa => Int, required => 0);
method getStandardResult() {
$self->finalResult(int(rand($self->dieType()) + 1));
return $self->finalResult();
}
}然后你可以:
use Dice;
Dice->new;https://stackoverflow.com/questions/20526354
复制相似问题