Extending attribute declaration syntax - best practice

[email protected] (Peter Shangov)
Newsgroups perl.moose
Message-ID <[email protected]>
Hi, 

MoooseX::Has::Options (https://metacpan.org/module/MooseX::Has::Options) adds a different syntax for declaring Moose attribute options, i.e. instead of:

    has 'foo', is => 'ro', isa => 'Str', lazy_build => 1;

it allows you to say:

    has 'foo', qw(:ro :lazy_build), isa => 'Str';

The current implementation is a little naive and works by directly replacing the 'has' subroutine in the calling package. Instead I want to make this work providing a new attribute metaclass that knows how to deal with the column-option syntax. The problem is that Moose::has() checks that the options list is even-sized, and will die with an odd number of column options.

So which of the following imperfect approaches would you recommend that I should take:

1. Replace the imported 'has' with a new one that does exactly the same but without the list size check. This means that the custom 'has' will go out of sync whenever the implementation of Moose::has() changes.

2. Apply an 'around' method modifier to Moose::has() that adds a dummy column option if the options list is not even-sized, and make the attribute metaclass handle it as a no-op. This means that the end will get to see this dummy option (e.g. in stack traces) and possibly get confused.

3. Apply an 'around' method modifier to Moose::has() that does all the column options pre-processing itself. This means not using an attribute metaclass at all and is only marginally better than the original approach.

Any ideas?


Thanks,

Peter
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.