j1n3l0 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I don't now how many of you have used MooseX-InsideOut but I was playing around with it the other day and discovered something odd. When you sub-class a standard Moose class with MooseX-InsideOut, unless you redefine the attributes in the child class, you cannot assign to them at object creation.

For example:

#!/usr/bin/env perl { package Normal::Moose::Class; use Moose; has 'name' => ( is => 'rw', default => __PACKAGE__, ); no Moose; 1; } { package Repeat::Attr::No; use MooseX::InsideOut; extends 'Normal::Moose::Class'; no MooseX::InsideOut; 1; } { package Repeat::Attr::Yes; use MooseX::InsideOut; extends 'Normal::Moose::Class'; has '+name' => ( default => __PACKAGE__, ); no MooseX::InsideOut; 1; } { package main; use 5.010_000; use strict; use warnings; use Test::More 'no_plan'; # Test a standard Moose class my $norm_moose = Normal::Moose::Class->new( name => 'normal-moose- +class' ); is( $norm_moose->name, 'normal-moose-class', 'normal-moose-class h +as the correct name' ); # Test the sub class with unmodified attribute my $io_moose = Repeat::Attr::No->new( name => 'repeat-attr-no' ); is( $io_moose->name, undef, 'insideout subclass does not assign to + attribute name on creation' ); $io_moose->name('repeat-attr-no'); is( $io_moose->name, 'repeat-attr-no', 'but will at any other time +' ); # Test the sub class with modified attribute my $child = Repeat::Attr::Yes->new( name => 'repeat-attr-yes' ); is( $child->name, 'repeat-attr-yes', 'unless you modify the attrib +ute in the class declaration' ); exit 0; } __END__ ok 1 - normal-moose-class has the correct name ok 2 - insideout subclass does not assign to attribute name on creatio +n ok 3 - but will at any other time ok 4 - unless you modify the attribute in the class declaration 1..4

As you can imagine, it would be really annoying to have to go through each attribute and modify them just so they work! (This does not seem to be a problem when sub-classing a standard perl hash-based class)

Is this desired behaviour? Is there some flag I'm missing somewhere (init_arg or the like...)? Could this be a bug? I'm just curious here =)

Smoothie, smoothie, hundre prosent naturlig!
  • Comment on Attributes in MooseX-InsideOut sub class not initiated on object creation
  • Download Code

Replies are listed 'Best First'.
Re: Attributes in MooseX-InsideOut sub class not initiated on object creation
by stvn (Monsignor) on Jul 16, 2008 at 15:59 UTC

    No, you are not missing something, and Yes, this is a known issue.

    The author of MooseX::InsideOut wrote it specifically to make it easier for Moose to inherit from non-Moose classes, we realized later on that it does not play well when inheriting from Moose classes. But as this was not part of the authors original requirements it hasn't been addressed. The reason for this issue goes deep into the MOP and how the accessors are set up vs. how the constructor does it's initialization. At this point fixing it is a matter of someone having the time available and the need for the feature.

    -stvn
      Thanks for that. Will stick to standard Moose classes for now =)


      Smoothie, smoothie, hundre prosent naturlig!