Excerpts from Evan Carroll's message of Tue Nov 03 11:38:47 -0500 2009: > On Tue, Nov 3, 2009 at 10:01 AM, Ovid <publiustemp-catal...@yahoo.com> wrote: > > When parsing documents, if my code finds more than one title in a document, > > it's an error because we don't know which title is needed. The user should > > be able to override this in the constructor and provide their own title > > (this is actually true for several attributes): > > So the mechanism you're looking for is (1) if the title is not set in > the constructor and (2) if it has already been set in runtime then (3) > die. This can be accomplished easy enough, with around and a > predicate, and it will ensure consistency throughout the program and > anything that subclasses it. > > has 'attribute' => ( isa => 'Str', is => 'ro', predicate => 'has_attribute' ); > > around 'attribute' => sub { > my ($next, $this, $key) = @_; > $self->has_attribute ? die 'exception' : $this->$next($key); > };
You need 'rw', not 'ro'. Also, you forgot to handle setting and getting separately. This might be preferable to my suggestion if your object's methods are being called mid-parse, rather than building up a data structure first and setting attributes based on it after parsing is done. Even then, I think I'd rather have a separate method than modify the accessor. hdp.