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

This is my first Moose experiment. Suggestions for improvement are welcome.

In this code, class ThreeD has a class variable $active_model. $active_model is a ThreeD::Model object which has an $active_entities variable.

On line 19, ThreeD::Model->new() is called, but the constructor is never actually run. Line 20 attempts to get the active_entities from the new model, but fails saying that there is no active_entities method in ThreeD::Model.

Expected output:

DEBUG DEBUG3 DEBUG2 DEBUG2a DEBUG1a

Actual output:

DEBUG Can't locate object method "active_entities" via package "ThreeD::Mode +l" at /home/john/workspace/TestOOP/MY/ThreeD.pm line 13.

The code:

#!/usr/bin/perl # package ThreeD; use Moose; use MooseX::ClassAttribute; use namespace::autoclean; class_has 'active_model' => (isa => 'Model', is => 'ro', default => sub {print "DEBUG\n"; my $model = ThreeD::Mo +del->new(); my $entities = $model- +>active_entities; print "DEBUG 1a\n"; return $model}); __PACKAGE__->meta->make_immutable; no Moose; no MooseX::ClassAttribute; 1; { package ThreeD::Model; use Moose; use namespace::autoclean; has 'active_entities' => (isa => 'Entities', is => 'ro', lazy => 0, default => sub {print "DEBUG 2\n"; my $entities = ThreeD::E +ntities->new(); print "DEBUG 2b\n"; return $entities}); around BUILDARGS => sub { my ($orig, $class, %args) = @_; print "DEBUG 3\n"; return $class->$orig(%args); }; __PACKAGE__->meta->make_immutable; 1; } { package ThreeD::Entity; use Moose; use namespace::autoclean; use MooseX::FollowPBP; my $my_index = 0; has 'entityID' => (isa => 'Int', is => 'ro', required => 1); around BUILDARGS => sub { my ($orig, $class, %args) = @_; $args{entityID} = $my_index; $my_index++; return $class->$orig(%args); }; __PACKAGE__->meta->make_immutable; 1; } { package ThreeD::Entities; use Moose; use namespace::autoclean; #use MooseX::FollowPBP; use OpenGL; has 'entity' => (isa => 'ArrayRef[ThreeD::Entity]', is => 'rw', default => sub {[]}); __PACKAGE__->meta->make_immutable; 1; } package main; use strict; use warnings; my $model = ThreeD->active_model; my $entities = $model->active_entities;

Replies are listed 'Best First'.
Re: Moose class not being constructed
by choroba (Cardinal) on Nov 16, 2015 at 13:59 UTC
    When I move the ThreeD::Model package definition to the top of the script, I'm getting a different error:
    Attribute (active_entities) does not pass the type constraint because: + Validation failed for 'Entities' with value ThreeD::Entities=HASH(0x +6011b0a80) at constructor ThreeD::Model::new ...

    So, I changed the constraint to

    has 'active_entities' => (isa => 'ThreeD::Entities',

    and...

    No more errors! (The output is still different, as it says DEBUG 2b, not 2a.)

    Update: Why does it work? has is a Perl subroutine, it doesn't run at compile time. You need to actually run the code at runtime for it to have effect.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Awesome! That works :-) :-)

      Thank you very much choroba!