Context:
Perl script initializes itself based on a user-supplied param-file, then acts on a user-supplied source-file to filter out data and do other operations.
The param-file file contains partial perl expression, which are later suppose to be eval
ed during runtime, for example:
match:!~ col:1 operand:1|2|3
match:=~ col:1 operand:[^123]
match:=! col:1 operand:^DATE
match:=~ col:1 operand:^(?:\s|DATE)
match:-~ col:1 operand:^\s
match:eq col:7 operand:CA
match:eq col:7 operand:DI
match:ne col:1 operand:ACCOUNT
match:ne col:1 operand:POSITIONS
match:== col:8 operand:999
match:!= col:8 operand:999
Hmm, and how about something like this? maybe later, but I need that too
match="ne list" '11, 71, 7'
Briefly, my perl will get the match operator from the user and then needs to filter out (or in) records from the source file based on the other params.
One and simple approach, is eval
:
next unless eval "$value $match $operand";
Now, given that I know that the $match
will always be the same, the use of eval
on EACH input of the source-file, sounds like an overkill.
if ($match eq '!~')
{
next unless $value !~ /$operand/o;
}
elsif ($match eq '=~')
{
next unless $value =~ /$operand/o;
}
elsif ($match eq 'eq')
{
next unless $value eq $operand;
}
...
And I was thinking of having a hash lookup, not sure how to do that. (I wonder on that too), also thinking of closures ...
I'm looking for best and most efficient approach?