in reply to Moose::Meta programming and derivative classes
Also, with Moose, you don't need to specify use strict . Moose enables strict and warnings automatically.
Do you mean something like this?
#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; { package MyApp::Object; use Moose; has greeting => (isa => 'Str', is => 'ro', required => 1); sub collection { ref(shift) . '::Collection' } { my %collection; sub BUILD { my ($self) = @_; my $class = $self->collection; Moose::Meta::Class->create( $class, methods => { push => sub { push @{ $collection{ +shift } }, $s +elf }, greet => sub { say $_->greeting for @{ $collection{ +shift } +} }, }, ); $class->push($self); } } __PACKAGE__->meta->make_immutable; } { package MyApp::ChildObject; use Moose; extends 'MyApp::Object'; __PACKAGE__->meta->make_immutable; } my $o = 'MyApp::Object'->new(greeting => 'Hallo'); $o->collection->greet; my $c1 = 'MyApp::ChildObject'->new(greeting => 'Ciao'); my $c2 = 'MyApp::ChildObject'->new(greeting => 'Salut'); $c1->collection->greet;
Instead of storing the collections in a lexical hash, you can create a singleton instance for each class.
($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,
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Moose::Meta programming and derivative classes
by JayBonci (Curate) on May 01, 2017 at 00:33 UTC | |
by Anonymous Monk on May 01, 2017 at 17:38 UTC |