[Catalyst] Catalyst::Plugin::FormValidator questions

Jules Bean jules at jellybean.co.uk
Fri Nov 18 11:38:11 CET 2005


John Wang wrote:
> On 11/18/05, *Jules Bean* <jules at jellybean.co.uk 
> 
>     sub my_custom_constraint {
>         my $c = shift;
>       return sub {
>         ...
>       }
>     }

[snip]

>     fieldname => my_custom_constrain($c)
> 
> 
> I tried that and couldn't get that to work. My understanding of DFV is 
> that it doesn't take variable parameters in the profile. The POD example 
> shows only constants being passed in the profile:
> 
>   constraint_methods => {
>     email                        => email(),

I don't understand your misunderstanding ;-)

There's nothing magic about this syntax. 'email()' is not somehow being 
passed into DFV 'as a sub' or anything like that. 'email()' is a 
subroutine call, it is executed at this point in your Perl code, and 
whatever it returns is passed into DFV. So to design a DFV constraint, 
you should write a subroutine which returns a subref. (Exactly as you 
suggested).

The subroutine can take whatever parameters you want it to take. You're 
the one that's calling it. The 'email()' example above is taking no 
parameters. My custom_constraint took one parameter, the context object 
which is then 'lexically captured' in the subref (more precisely, in the 
*closure*) that the subroutine returns.

If you still can't get it to work I'll try to knock up a minimal working 
example.

Jules




More information about the Catalyst mailing list