无法设置类别'角色属性

发布于 2024-09-15 04:08:37 字数 629 浏览 1 评论 0原文

首先,我不太确定需要包含多少信息,因为我很难追踪这个问题的根源。

我有一个 Moose 角色,它有一个子例程(以及其他一些东西)尝试为这样的类设置属性:

$genre = Movie::Genre->new({
    genreName => 'Drama',
    genreID => '1'
                 });

问题是,它没有。紧随其后的 $genre 转储表明它仍然是空的:

$genre: bless( {}, 'Movie::Genre' )

仍然陌生,当我执行 THE在我的测试文件中完全相同的行,它按预期与此转储一起工作:

$genre: bless( {
             genreID => '1',
             genreName => 'Drama'
           }, 'Movie::Genre' )

我正在努力寻找使这两行代码不同的原因,导致一行工作,另一行失败。

关于什么条件会导致第一个示例失败并允许第二个示例成功的任何想法?如有必要,我很乐意提供更多背景信息。谢谢!

First off, I'm not really sure how much information is necessary to include because I'm having a really hard time tracing the origin of this problem.

I have a Moose role with a subroutine that (along with a few other things) tries to set the attributes for a class like this:

$genre = Movie::Genre->new({
    genreName => 'Drama',
    genreID => '1'
                 });

The problem is, it doesn't. The dump of $genre immediately after, indicates that it's still empty:

$genre: bless( {}, 'Movie::Genre' )

Stranger still, when I execute THE EXACT SAME LINE in my test file, it works as expected with this dump:

$genre: bless( {
             genreID => '1',
             genreName => 'Drama'
           }, 'Movie::Genre' )

I'm struggling to find what makes these two lines of code different, causing one to work and one to fail.

Any ideas as to what conditions would cause the first example to fail and allow the second to succeed? I'd be happy to provide more context if necessary. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三月梨花 2024-09-22 04:08:38

该行只是将这些参数传递给 Movie::Genre 构造函数。由构造函数决定如何处理它们。

听起来(在角色中)调用是在 Movie::Genre 类获取名为 genreNamegenreID 的属性之前执行的。默认情况下,Moose 构造函数会忽略它们无法识别的任何参数,因此不会生成警告。

您的测试文件必须在将属性添加到 Movie::Genre 后进行该调用。

我们必须查看更多代码才能弄清楚为什么会发生这种情况。

That line simply passes those parameters to the Movie::Genre constructor. It's up to that constructor to decide what to do with them.

It sounds like that call (in the role) is getting executed before the Movie::Genre class has acquired attributes named genreName and genreID. By default, Moose constructors ignore any parameters they don't recognize, so this doesn't generate a warning.

Your test file must be making that call after the attributes have been added to Movie::Genre.

We'd have to see more of the code to figure out exactly why this is happening.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文