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


In reply to Dynamically Creating a Code reference by Herkum

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.