Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I might get raked over the coals for this, but when in doubt, lie :)</p> <pre><code>package MyMoose; use strict; use warnings; use Carp 'confess'; sub import { my $caller = caller; eval &lt;&lt;"END" or confess("Loading MyMoose failed: $@"); package $caller; use Moose; use MooseX::StrictConstructor; use MooseX::FollowPBP; 1; END } 1; </code></pre> <p>By doing that, you're evaling the use statements into the calling package. In other words, you're lying to them about what class they are used in.</p> <p>And here you declare your person:</p> <pre><code>package MyPerson; use MyMoose; has first_name =&gt; ( is =&gt; 'ro', required =&gt; 1 ); has last_name =&gt; ( is =&gt; 'rw', required =&gt; 1 ); 1; </code></pre> <p>And tests!</p> <pre><code>use lib 'lib'; use MyPerson; use Test::Most; throws_ok { MyPerson-&gt;new( first_name =&gt; 'Bob' ) } qr/\QAttribute (last_name) is required/, 'Required attributes should be required'; throws_ok { MyPerson-&gt;new( first_name =&gt; 'Billy', last_name =&gt; 'Bob', what =&gt; '?', ); } qr/\Qunknown attribute(s) init_arg passed to the constructor: what/, '... and unknown keys should throw an error'; my $person; lives_ok { $person = MyPerson-&gt;new( first_name =&gt; 'Billy', last_name =&gt; 'Bob' ) } 'Calling the constructor with valid arguments should succeed'; isa_ok $person, 'MyPerson'; can_ok $person, qw/get_first_name get_last_name set_last_name/; ok !$person-&gt;can("set_first_name"), '... but we should not be able to set the first name'; done_testing; </code></pre> <p>And the test results:</p> <pre><code>ok 1 - Required attributes should be required ok 2 - ... and unknown keys should throw an error ok 3 - Calling the constructor with valid arguments should succeed ok 4 - The object isa MyPerson ok 5 - MyPerson-&gt;can(...) ok 6 - ... but we should not be able to set the first name 1..6 </code></pre> <p>Let's keep this our little secret, shall we? :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload