Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

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

You can use types to constrain the values of attributes, see Moose::Manual::Types and Moose::Util::TypeConstraints. For example, you could use an anonymous type to ensure that the value of Month is between 1 and 12.

If you need to perform more complex checking of your arguments, then you can define a BUILD method (see Moose::Manual::Construction).
package Appointment; use Moose; # automatically turns on strict and warnings use Moose::Util::TypeConstraints; my @freq_valid = (qw/OneTime Daily Monthly Quarterly/); has 'Month' => ( is => 'ro', isa => subtype( 'Int' => where { $_ > 0 and $_ < 13 +} ), required => 1, ); has 'Day' => ( is => 'ro', isa => 'Int', required => 1, ); has 'Freq' => ( is => 'ro', isa => 'Str', default => 'OneTime', ); sub BUILD { my $self = shift; if ($self->Month == 12 and $self->Day == 1){ die "I'm having a bad Moose day."; } } 1;
UPDATE: Here is an example of using a subtype to check the values of your Freq attribute.
package Appointment; use Moose; # automatically turns on strict and warnings use Moose::Util::TypeConstraints; use List::Util qw/any/; my @freq_valid = (qw/OneTime Daily Monthly Quarterly/); subtype 'AppointmentFreq' => as 'Str' => where { my $arg = $_; any{ $_ eq $arg } @freq_valid } => message { 'The Freq you provided is not valid' }; has 'Month' => ( is => 'ro', isa => subtype( 'Int' => where { $_ > 0 and $_ < 13 +} ), required => 1, ); has 'Day' => ( is => 'ro', isa => 'Int', required => 1, ); has 'Freq' => ( is => 'ro', isa => 'AppointmentFreq', default => 'OneTime', ); sub BUILD { my $self = shift; if ($self->Month == 12 and $self->Day == 1){ die "I'm having a bad Moose day."; } } 1;

In reply to Re: Moose: checking parms in object construction by kevbot
in thread Moose: checking parms in object construction by Marshall

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 admiring the Monastery: (5)
As of 2024-04-25 12:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found