None of the Moose built-in types treat overloaded objects as the non-object types they overload.

So for example, an object overloading "" will not pass the Str type constraint. And objects overloading 0+ will not pass Num or Int.

Whether you think this is a good design decision or not, it's the way Moose works, and changing it now would likely break a lot of stuff that relies on the existing behaviour.

Luckily, you don't need to use the built-in Moose Bool type. You can define your own type constraints.

Moose::Util::TypeConstraints makes this possible. MooseX::Types makes it a bit easier. But (and I'm biased on this!), I'd recommend using Types::Standard which is a type constraint library that works with Moose, Mouse, and Moo, but doesn't require any of them. Here's one way you could achieve your aims with it:

{ package My::Package; use Moose; use Types::Standard qw( Bool Overload ); has attr => ( is => 'ro', isa => Bool | Overload["bool"], ); __PACKAGE__->meta->make_immutable; }

Or even:

{ package My::Package; use Moose; use Types::Standard qw( Bool Overload ); has attr => ( is => 'ro', isa => Bool->plus_coercions( Overload["bool"], sub { !!$_ +} ), coerce => 1, ); __PACKAGE__->meta->make_immutable; }

In reply to Re: Cpanel::JSON::XS::true and Moose don't play well together by tobyink
in thread Cpanel::JSON::XS::true and Moose don't play well together by choroba

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.