Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I'd split out type constraint definitions into a separate type library module. Here's a quick example:

use strict; use warnings; # thanks.pm is a shim for declaring multiple packages in the same file +. # It's on CPAN if you want it. Otherwise, just split them into separat +e # files. no thanks qw( MyApp::Types MyApp::Printer MyApp::Printer::ThreeColour MyApp::Printer::FourColour ); BEGIN { package MyApp::Types; use Type::Library -base, -declare => qw( ThreeColour FourColour ); use Type::Utils; use Types::Standard -types; use Types::ReadOnly -types; declare ThreeColour, as Locked[ Dict[ cyan => Optional[Num], magenta => Optional[Num], yellow => Optional[Num], ] ], coercion => 1; # black is referred to as "key" in CYMK printing. presumably becau +se # using a name that abbreviated to "B" would be confused with "blu +e". declare FourColour, as Locked[ Dict[ @{ ThreeColour->parent->wrapped->parameters }, # voodoo? key => Optional[Num], ] ], coercion => 1; }; BEGIN { package MyApp::Printer; use Moose::Role; # Let's assume the printer is only capable of printing one pixel. has pixel => (is => 'ro'); sub dump { require Data::Dumper; Data::Dumper::Dumper(@_); } }; BEGIN { package MyApp::Printer::ThreeColour; use MyApp::Types -types; use Moose; with qw(MyApp::Printer); has '+pixel' => (isa => ThreeColour, coerce => 1); }; BEGIN { package MyApp::Printer::FourColour; use MyApp::Types -types; use Moose; with qw(MyApp::Printer); has '+pixel' => (isa => FourColour, coerce => 1); }; my $printer = 'MyApp::Printer::FourColour'->new( pixel => { cyan => 9.4, magenta => 7, yellow => 4 }, ); print($printer->dump); $printer->pixel->{key} = 9; # ok print($printer->dump); $printer->pixel->{black} = 6; # dies
use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

In reply to Re^3: constraining the keys of a HashRef in Moose by tobyink
in thread constraining the keys of a HashRef in Moose by tomgracey

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-19 23:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found