On 5/24/2010 11:01 AM, Jonathan Swartz wrote:
> There is no easy way to do this. Check out Params::Validate or
> MooseX::Params::Validate.
>
> On May 23, 2010, at 12:06 AM, Andreas Falch wrote:
>
>> Hi,
>>
>> Is there an easy way to add parameter validation to %args?
>>
>> E.g. based on the example in the Mason book chapter 2, can I mandate
>> that a scalar is one the elements in a list, otherwise an error is thrown?
>>
>> Pseudo-code:
>>
>> <%args>
>> $size => [ 'small', 'medium', 'large']
>> </%args>


If you are running a recent version of Perl, it is a simple matter to do 
the following:

<%init>
if (not $size ~~ [ qw{ small medium large } ] ) {
   # Carp, Croak, Wretch, ...
}
</%init>


If your version of Perl is old enough that it does not have the smart 
match operator (~~), you can still do the following, but it is more guru 
level than you may want future maintainers to be:

<%init>
if (not exists { map{ $_=>'' } qw{ small medium large } }->{$size}) {
   # Carp, Croak, Barf, ...
}
</%init>

------------------------------------------------------------------------------

_______________________________________________
Mason-users mailing list
Mason-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mason-users

Reply via email to