Herkum has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to dynamically build a Data::FormValidator profile. A short profile would look like this,
my $profile = { required => qw[my_name], constraint_methods => { my_name => sub { my $dfv = shift; my $var = shift; return if ($var !~ /\d+/); return 1; } } }
However, I want take a string and make it into closure in my profile. I am missing something however. If I do this,
$code =<<'END' my $dfv = shift; my $var = shift; return if ($var !~ /\d+/); return 1; END ; my $profile->{constraint_methods}{my_name} = sub { eval $code }; # Doe +s not work
Update: Made the $code not interpolate.
Update No 2: The code above is not working, in that the DFV object is not being returned first to @_.
my $profile->{constraint_methods}{my_name} = sub { use Data::Dump qw(d +ump); warn "PARAMS " . dump(@_) . "\n"; eval $code }; # Does not work
This will dump the DFV object from @_. It seems the eval does not get the whole @_ for some reason. I don't understand why
Update No 3: OK, I found the answer and it had nothing to do with eval, which was working but rather with the validation. There is also a field_filter option used, it does not pass the DFV object, I assumed that it did, I would alter the field value, which screwed up the validation and the constraint code was not run, which is where I thought I was having problems.
Thanks for the help
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Dynamically Creating a Code reference
by eric256 (Parson) on Jun 15, 2006 at 21:53 UTC | |
by Herkum (Parson) on Jun 16, 2006 at 13:06 UTC | |
|
Re: Dynamically Creating a Code reference
by rblasch (Monk) on Jun 15, 2006 at 22:01 UTC | |
by Herkum (Parson) on Jun 16, 2006 at 12:25 UTC | |
|
Re: Dynamically Creating a Code reference
by ioannis (Abbot) on Jun 16, 2006 at 00:10 UTC | |
|
Re: Dynamically Creating a Code reference
by Tanktalus (Canon) on Jun 15, 2006 at 23:37 UTC | |
| |
|
Re: Dynamically Creating a Code reference
by bart (Canon) on Jun 16, 2006 at 07:30 UTC | |
by Herkum (Parson) on Jun 16, 2006 at 12:38 UTC | |
by bart (Canon) on Jun 16, 2006 at 13:07 UTC | |
by Herkum (Parson) on Jun 16, 2006 at 13:34 UTC | |
|
Re: Dynamically Creating a Code reference
by badaiaqrandista (Pilgrim) on Jun 16, 2006 at 13:01 UTC |