develooper Front page | perl.moose | Postings from January 2010

TypeConstraints message

Thread Next
From:
Dermot
Date:
January 19, 2010 04:34
Subject:
TypeConstraints message
Message ID:
9c7446e71001190434y6fd95acev99c31f70e32aac58@mail.gmail.com
Hi,

I'm using the enum() constraints for a attribute. It works fine like this:

has 'index_type' => (
                       is   => 'ro',
                       isa  => enum($_, qw(for bar baz)),
                       required => 1,
              );

But I'd like to provide a friendlier message then "Attribute
(index_type) does not pass the type constraint because:...5 more lines
of croak"

At this point I got stuck. I cannot add 'message' to the attribute
definition. It looks like I have to create a subtype but I can't seem
to get enum to work correctly within my subtype. This snip below will
allow ANY value.


use Moose;
use Moose::Util::TypeConstraints;

subtype 'IndexType'
                => as 'Str',
                => enum( $_, qw[foo bar baz]);
                => message { "index_type ($_) must be one of ". join
', ', qw[foo bar baz] };

has 'index_type'    => (
                isa         => 'IndexType',
                is          => 'ro',
                required => 1,
    );


my $indexer = MyApp::Local::Indexer->new(index_type=>'foobarquin');
print "Index=$index\n";

# Index=MyApp::Local::Indexer=HASH(0x1a8fbc0)

Can anyone point me in the right direction please?
Thanks,
Dp.

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About