develooper Front page | perl.moose | Postings from June 2011

Re: Moose Type Constraints violations to warnings?

Thread Previous | Thread Next
Chris Weyl
June 11, 2011 11:22
Re: Moose Type Constraints violations to warnings?
Message ID:
On Fri, Jun 10, 2011 at 3:06 AM, Ovid <>wrote:

> Without using signal handlers, is there some way I can convert that
> exception into a warning except when something like $ENV{HARNESS_ACTIVE} is
> true? Preferably on a per-attribute basis?

With the caveat that I haven't checked for any larger implications, and I'm
not making any value judgements here as to the wisdom of selectively
disabling the tc mechanism:

This could be done in Moose by creating and selectively applying an
attribute trait that overrides the TC inline code generation methods
(_inline_tc_code(), _inline_check_constraint(), etc) and others related to
enforcing the type constraints.  You may also need a constructor trait for
your class' metaclass that tells it to not explode when it hits a tc
violation.  Moose itself provides the tools to make this a little easier;
see, e.g. MooseX::AutoDestruct for an example of traits that do this sort of

Not being terribly familiar with Mouse (except that one last year that ate a
perfectly good bag of rice), I'm not sure how to do something like this over

I think Edward/Karen are on the right track, though:  If it's reasonable to
do so, building out a more tolerant type library with complimentary
coercions that help "correct" the data (or at least are broad enough to
never be violated) is probably more useful to you in the long run than
altering the tc system.  I'm not in any position to be making that judgement
for you, however, and an attribute trait to modify the tc behaviour
certainly looks to be feasible.


Chris Weyl
Ex astris scientia

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About