开发者

Not Able to Set Class' Attributes in Role

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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜