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 | |
by SwaJime (Scribe) on Nov 16, 2015 at 14:10 UTC |