handles => { birth_year => 'year' },
);
- subtype 'ShirtSize'
- => as 'Str'
- => where { /^(?:s|m|l|xl|xxl)$/i }
- => message { "$_ is not a valid shirt size (s, m, l, xl, xxl)" };
+ enum 'ShirtSize' => qw( s m l xl xxl );
has shirt_size => (
is => 'rw',
default => 'l',
);
-This is a fairly simple class with three attributes. We also define a
-type to validate t-shirt sizes because we don't want to end up with
-something like "blue" for the shirt size!
+This is a fairly simple class with three attributes. We also define an enum
+type to validate t-shirt sizes because we don't want to end up with something
+like "blue" for the shirt size!
package User;
use DateTime;
use DateTime::Format::Natural;
-
sub new {
my $class = shift;
my %p = ref $_[0] ? %{ $_[0] } : @_;
defined $shirt_size
or confess 'shirt_size cannot be undef';
- $shirt_size =~ /^(?:s|m|l|xl|xxl)$/
+ my %sizes = map { $_ => 1 } qw( s m l xl xxl );
+
+ $sizes{$shirt_size}
or confess "$shirt_size is not a valid shirt size (s, m, l, xl, xxl)";
}
use base 'Person';
-
sub new {
my $class = shift;
my %p = ref $_[0] ? %{ $_[0] } : @_;