Creating a right dynamic class using Moose :: Meta :: Class or Class :: MOP :: Class
I would like to implement a simple method chaining as these methods are supposed to return objects. Since my code is already used by Moose, and my future classes are almost empty and only return one attribute with an instance of another class, I decided to generate them programmatically using Moose :: Meta :: Class (this is a subclass of Class :: MOP :: Class ). First time working with Moose metaclasses, I wrote some simple code to try:
package Cat;
use Moose;
package Generator;
use Moose;
sub generate {
Class::MOP::Class->create(
'Siberian' => (
methods => {
echo => sub { print 'yeah!' }
}
)
);
Class::MOP::Class->create(
'Tiger' => (
attributes => [
Class::MOP::Attribute->new('Siberian' => (
default => sub { require Siberian; Siberian->new; }
)),
],
)
);
Cat->meta->add_attribute(Class::MOP::Attribute->new(
Tiger => (
default => sub { require Tiger; Tiger->new }
)
));
print "Generation done!\n";
}
generate();
package main;
use Generator;
my $a = Cat->new;
warn $a->Tiger->Siberian->echo; # must print 'yeah!' but prints Can't locate object method "new" via package "Tiger" at dynamic.pl line 33
Instead of what is expected yeah!
in the output, I see
Cannot find method of object "new" via package "Tiger" in dynamic.pl line 33
What's wrong?
source to share
Who will be responsible for creating the constructor?
Another way is to use metaclassical variables directly.
https://gist.github.com/akzhan/421579163dfc86f570809f50d239800f
source to share