Hi krautcat, there are no silly questions, only silly answers ;-)

yes, in this case "an exception" means that the program exits with a fatal error returned by the type checking sub if the value is not valid. When it dies it "throws an exception".

Now, as to your other question. Yes, you can use the Types to validate elsewhere. You should spend some time reading in the docs of Type::Tiny and friends (which is spread through several files).

One of the simplest ways is to make use of the auto-defined is_<type_name>() functions that Type::Tiny defines and provides in Types::Standard:

Package:

package Krautcat; use strict; use warnings; use Moo; use MooX::TypeTiny;: use Types::Standard qw/ Str is_Str /; has foo => ( is => 'ro', isa => Str, required => 1 ); has baz => ( is => 'rw', ); sub qux { my $self = shift; my $val = shift; die 'not a string' if not is_Str $val; $self->baz( $val ); return { baz => $self->baz }; } 1;
Script:
use strict; use warnings; use Test::Most; use_ok 'Krautcat', 'Loaded class'; throws_ok( sub { my $obj = Krautcat->new() }, qr/Missing required arguments: foo/, 'empty params throws ok', ); dies_ok( sub { my $obj = Krautcat->new( foo => [42] ) }, 'non-string param for constructor dies ok', ); like( $@, qr/\QReference [42] did not pass type constraint "Str"\E/, 'assertion failure message looks ok', ); my $obj = new_ok( 'Krautcat' => [ foo => 'bar' ], 'obj with valid constructor params', ); is( $obj->foo, 'bar', 'attr has correct val from constructor' ); throws_ok( sub { $obj->qux( [42] ) }, qr/not a string/, 'non-string param for qux() throws ok', ); lives_and( sub { is_deeply $obj->qux('blorg'), { baz => 'blorg' } }, 'string param for qux() validates ok', ); done_testing;
Output:
prove -v 1199413.pl 1199413.pl .. ok 1 - use Krautcat; ok 2 - empty params throws ok ok 3 - non-string param for constructor dies ok ok 4 - assertion failure message looks ok ok 5 - 'obj with valid constructor params' isa 'Krautcat' ok 6 - attr has correct val from constructor ok 7 - non-string param for qux() throws ok ok 8 - string param for qux() validates ok 1..8 ok All tests successful. Files=1, Tests=8, 0 wallclock secs ( 0.01 usr 0.01 sys + 0.08 cusr + 0.01 csys = 0.11 CPU) Result: PASS

This is the most simple feature and example I can think of; there is a lot more to discover!


The way forward always starts with a minimal test.

In reply to Re^3: Moo's coerce and isa difference by 1nickt
in thread Moo's coerce and isa difference by krautcat

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.