I'm not sure what the expected output is, but I'll probably avoid using eval.
#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; sub evaluate_row { my ($rules, $row) = @_; for my $column (keys %$row) { if (exists $rules->{$column}) { return 0 unless $rules->{$column}($row->{$column}); } } return 1 } sub is_greater_to_0 { $_[0] > 0 } sub is_even { 0 == $_[0] % 2 } my $rules = {a => \&is_greater_to_0, b => \&is_even}; my @rows = ({a => 1, b => 2}, {a => 0, b => 2}, {a => 1, b => 3}, {a => 0, b => 3}); for my $row (@rows) { say evaluate_row($rules, $row) ? 'ACCEPTED' : 'REJECTED'; }

In fact, as you'll probably use the same rules for all the rows, initialising an object with the rules as the constructor argument makes even more sense. It moves us a bit farther from the initial example, but here it is:

#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; { package My::RowChecker; sub new { my ($class, $rules) = @_; die 'Rules should be a hash' unless ref {} eq ref $rules; return bless {rules => $rules}, $class } sub check { my ($self, $row) = @_; for my $column (keys %$row) { if (exists $self->{rules}{$column}) { return 0 unless $self->{rules}{$column}($row->{$column +}); } } return 1 } } sub is_greater_to_0 { $_[0] > 0 } sub is_even { 0 == $_[0] % 2 } my $checker = 'My::RowChecker'->new({a => \&is_greater_to_0, b => \&is_even}); my @rows = ({a => 1, b => 2}, {a => 0, b => 2}, {a => 1, b => 3}, {a => 0, b => 3}); for my $row (@rows) { say $checker->check($row) ? 'ACCEPTED' : 'REJECTED'; }

You can also use an object orientation system to implement the class, e.g. Moo:

#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; { package My::RowChecker; use Moo; use Types::Standard qw( HashRef CodeRef ); has rules => (is => 'ro', required => 1, isa => HashRef[CodeRef]); sub check { my ($self, $row) = @_; for my $column (keys %$row) { if (exists $self->rules->{$column}) { return 0 unless $self->rules->{$column}($row->{$column +}); } } return 1 } } sub is_greater_to_0 { $_[0] > 0 } sub is_even { 0 == $_[0] % 2 } my $checker = 'My::RowChecker'->new(rules => {a => \&is_greater_to_0, b => \&is_even}); my @rows = ({a => 1, b => 2}, {a => 0, b => 2}, {a => 1, b => 3}, {a => 0, b => 3}); for my $row (@rows) { say $checker->check($row) ? 'ACCEPTED' : 'REJECTED'; }

Updated: Used named subs.
Update 2: Added the OO examples.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

In reply to Re: column replacement evaluation on dataset by choroba
in thread column replacement evaluation on dataset by Anonymous Monk

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.