Many of the issues you point out here with XML::Toolkit are a symptom of it's maturity and the conscious decision to punt on handling ambiguous cases. Take for example the collection interface you illustrate:
my $document = MyApp::Note->new( to_collection => [MyApp::To->new(text => 'Bob')], from_collection => [MyApp::From->new(text => 'Alice')], headings => [MyApp::Heading->new(text => 'Secret' )], body_collection => [MyApp::Body->new(text=>'Shh!')], )
XML::Toolkit has no way to know the number of to child elements, so it defaults to assuming "many". If you know that you're only ever going to have one to/from/heading/body then you can re-write the class to change these from ArrayRef types to Object types.
The second object invocation can be eliminated by adding a coercion.
coerce 'MyApp::To' => from 'Str' => via { MyApp::To->new(text => $_) }; coerce 'MyApp::From' => from 'Str' => via { MyApp::From->new(text => $_) }; coerce 'MyApp::Heading' => from 'Str' => via { MyApp::Heading->new(text => $_) }; coerce 'MyApp::Body' => from 'Str' => via { MyApp::Body->new(text => $_) };
Combing both of these should get you identical syntax to your XML::Element::Tolol example. Unfortunately this is currently a manual post-processing of the files that XML::Toolkit generates. I could conceptually be able to build the coercions for Text nodes from Str programmatically during compilation and then generate a Type Library that is included in the generated classes. I simply have never had this need in the projects I've used XML::Toolkit on.
Long term your compiler-compiler idea is similar to what I envision XML::Toolkit becoming. I think it should be possible to generate a Schema from XML::Toolkit generated Moose classes. This would allow you to bootstrap to get the kind of clarity you are wanting because you could generate an Schema from an example document, modify the schema to correctly reflect anything not properly reflected in the example document, then generate the final classes from the Schema.
The problem is I don't have a billable project to implement this. Is this something people would be willing to fund a grant for?
In reply to Re: generating XML with data structures and Perl classes - the XML::Element::Tolol approach
by Anonymous Monk
in thread generating XML with data structures and Perl classes - the XML::Element::Tolol approach
by metaperl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |