<div dir="ltr">Hello TPM,<div><br></div><div>Just an interesting note to those who saw my Type::Tiny talk a few months back.</div><div><br></div><div>A new version of Moo was just released and I noted that to do a coercion with Type::Tiny on an attribute you had to do the following:</div>
<div><br></div><div><font face="courier new, monospace">has lines => (</font></div><div><font face="courier new, monospace"> is => 'rw',</font></div><div><font face="courier new, monospace"> isa => MyType</font></div>
<div><font face="courier new, monospace"> coerce => MyType->coercion,</font></div><div><font face="courier new, monospace">);</font></div><div><br></div><div>This was a bit restrictive because you HAD to create a named type to get the coercion because of the need for the second call. However, Moo's newest release now supports <font face="courier new, monospace">coerce => 1</font> where the <font face="courier new, monospace">isa</font> is a blessed object with a coerce or coercion method which means you can do this:</div>
<div><br></div><div><font face="courier new, monospace">has lines => (</font></div><div><font face="courier new, monospace"> is => 'rw',</font></div><div><font face="courier new, monospace"> isa => MyType,</font></div>
<div><font face="courier new, monospace"> coerce => 1,</font></div><div><font face="courier new, monospace">);</font></div><div><br></div><div>This is great, because this means you can now define a type using the base types (remember, never add a coercion directly to a base type), on the fly with the <font face="courier new, monospace">plus_coercions</font> and have it work:</div>
<div><br></div><div><font face="courier new, monospace">use Types::Standard qw{ ArrayRef Split };</font></div><div><font face="courier new, monospace">has lines => (</font></div><div><font face="courier new, monospace"> is => 'rw',</font></div>
<div><font face="courier new, monospace"> isa => ArrayRef->plus_coercions( Split[ qr{\n} ] ),</font></div><div><font face="courier new, monospace"> coerce => 1,</font></div><div><font face="courier new, monospace">);</font></div>
<div><font face="courier new, monospace"><br></font></div><div><span style="font-family:arial">This will require an arrayref, but will coerce a string into an arrayref. Now you need not do simple one-time only used types in a library.</span></div>
<div><span style="font-family:arial"><br></span></div><div><span style="font-family:arial">D</span><br></div><div><div><div>--</div><a href="mailto:dave.s.doyle@gmail.com" target="_blank">dave.s.doyle@gmail.com</a></div>
</div></div>