Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Cannot coerce from Bool to Str

by tobyink (Canon)
on Sep 08, 2018 at 20:39 UTC ( [id://1221959]=note: print w/replies, xml ) Need Help??


in reply to Cannot coerce from Bool to Str

As per choroba's answer, coercions are only triggered if the value fails the type constraint, and "1" is a valid string.

You could use Enum["yes","no"] as your type constraint instead of Str.

use 5.026; use Test::Most; { package Thing; use Moose; use Types::Standard qw( Bool Enum ); has delete => ( coerce => 1, is => 'ro', isa => Enum->of(qw/yes no/)->plus_coercions( Bool, q{ $_ ? +'yes' : 'no' } ), ); } { cmp_methods( new_ok( Thing => [ delete => 1 ] ), [ delete => 'yes' ], 'attributes are coerced as expected', ); } done_testing;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1221959]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-24 08:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found