views:

32

answers:

1

Hello all,

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!

+2  A: 

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.

cjm
Thank you - you were exactly right. I had a long chain of roles and after some examination of who calls who first and some reshuffling, the problem was fixed.
Ryan
The recommended way to compose multiple roles (a long chain) is with a single call `with qw(Role1 Role2 Role3);`. This way the composition is unordered shouldn't affect you. However in reality this may not work every time and so (due to a quirk of the implementation) multiple calls to `with()` can create an ordered composition.
perigrin