Re: Java annotation/.NET attributes in Moose

[email protected] (Sherwin Daganato)
Newsgroups perl.moose
Message-ID <CALvYtFkR_a6ix_waF8vf4e3+NQgch7355M6cUYb2BRd83ctPjw@mail.gmail.com>
On Wed, Oct 19, 2011 at 7:28 PM, Stevan Little
<[email protected]> wrote:
> Annotations/Attributes are kind of the bastard love child of compiler hints and macros, they make less sense in a highly dynamic language like Perl.
>
> Can you explain what it is you want to do with annotations/attributes? And perhaps we can point you in a more perl-ish direction.

Honestly I have an ambitious plan to create a module using Moose that
will start out small and eventually evolve into something like what
.Net XML Serialization[1], and JAXB[2] offer -- XML binding and
serialization, and class generation from XML schema.  Much like what
XML::Toolkit has originally intended to do, but I'd like it to follow
the API -- which unfortunately depends on Annotation/Attributes -- of
.Net, JAXB, or Simple[3] as closely as possible because I find it
simpler and easier to understand.

I'm currently thinking of doing something like this for the XML schema
described in [1]:

package Books;

use namespace::autoclean;
use XML::Something;
use Moose -traits => [
    'XMLType'        => { namespace =>
'urn:xmlns:25hoursaday-com:my-bookshelf' },
    'XMLRootElement' => { name => 'books', namespace =>
'urn:xmlns:25hoursaday-com:my-bookshelf' },
];

has 'book' => (
    traits   => [ 'Array', 'XMLElement' ],
    is       => 'rw',
    isa      => 'ArrayRef[BookType]',
    required => 1,
);

__PACKAGE__->meta->make_immutable;

# =-=-=-=-=-=-=-=-=-=-=-=-

package BookType;

use namespace::autoclean;
use XML::Something;
use Moose -traits => [
    'XMLType' => { namespace =>
'urn:xmlns:25hoursaday-com:my-bookshelf', order => [ 'title',
'author', 'publicationdate' ] },
];

has 'title' => (
    traits   => [ 'XMLElement' ],
    is       => 'rw',
    isa      => 'Str',
    required => 1,
);
has 'author' => (
    traits   => [ 'XMLElement' ],
    is       => 'rw',
    isa      => 'Str',
    required => 1,
);
has 'publicationdate' => (
    traits   => [ 'XMLElement' => { name => 'publication-date',
datatype => 'date' } ],
    is       => 'rw',
    isa      => 'DateTime',
    required => 1,
);
has 'publisher' => (
    traits   => [ 'XMLAttribute' ],
    is       => 'rw',
    isa      => 'Str',
);
has 'onloan' => (
    traits   => [ 'XMLAttribute' => { name => 'on-loan' } ],
    is       => 'rw',
    isa      => 'Str',
);

__PACKAGE__->meta->make_immutable;

# =-=-=-=-=-=-=-=-=-=-=-=-


[1] http://msdn.microsoft.com/en-us/library/ms950721.aspx
[2] http://jaxb.java.net/tutorial/section_1_3-Hello-World.html
[3] http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.