gpt4 book ai didi

perl - 我可以在 Dancer 中实例化一个对象以返回要显示的值吗?

转载 作者:行者123 更新时间:2023-12-04 15:34:39 24 4
gpt4 key购买 nike

我的 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();
}
}

最佳答案

我要说你忘记了package Dice在您的 Dice.pm ,但是在阅读了 Moops 之后,我对命名空间感到困惑。

我们来看看documentation for Moops .

If you use Moops within a package other than main, then package names used within the declaration are "qualified" by that outer package, unless they contain "::". So for example:

package Quux;
use Moops;

class Foo { } # declares Quux::Foo

class Xyzzy::Foo # declares Xyzzy::Foo
extends Foo { } # ... extending Quux::Foo

class ::Baz { } # declares Baz


如果 class DiceDice.pm它实际上会变成 Dice::Dice如果我没看错的话。所以你必须 use Dice并使用 Dice::Dice->new 创建您的对象.

为了制作包 DiceDice.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;

关于perl - 我可以在 Dancer 中实例化一个对象以返回要显示的值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20526354/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com