我有一个很大的散列多层次深,我想要把这个散列成一组驼鹿类。
哈希看起来是这样的:
my %hash = (
company => {
id => 1,
name => 'CorpInc',
departments => [
{
id => 1,
name => 'Sales',
employees => [
{
id => 1,
name => 'John Smith',
age => '30',
},
],
},
{
id => 2,
name => 'IT',
employees => [
{
id => 2,
name => 'Lucy Jones',
age => '28',
},
{
id => 3,
name => 'Miguel Cerveza',
age => '25',
},
],
},
],
}
);
和驼鹿类:
package Company;
use Moose;
has 'id' => (is => 'ro', isa => 'Num');
has 'name' => (is => 'ro', isa => 'Str');
has 'departments' => (is => 'ro', isa => 'ArrayRef[Company::Department]');
1;
package Company::Department;
use Moose;
has 'id' => (is => 'ro', isa => 'Num');
has 'name' => (is => 'ro', isa => 'Str');
has 'employees' => (is => 'ro', isa => 'ArrayRef[Company::Person]');
1;
package Company::Person;
use Moose;
has 'id' => (is => 'ro', isa => 'Num');
has 'first_name' => (is => 'ro', isa => 'Str');
has 'last_name' => (is => 'ro', isa => 'Str');
has 'age' => (is => 'ro', isa => 'Num');
1;
请告诉我把这个散列成Company对象的最佳方式?
到目前为止,我所考虑的选项包括:
- 手动环路%哈希,发现最深的“类”(如人),首先要创建这些,然后手动添加这些新创建的高级别类(部),等等。
- 某种强制的功能添加到每一个类,它允许我做一些像公司 - >新(%哈希),使每一个类来创建自己的“子类”(通过强制)
- 该散列%转换成类似于MooseX ::存储可以序列化到一个结构,然后用MooseX ::存储到instatiate了我的一切......
任何其他的想法或建议?