http://qs321.pair.com?node_id=596988

xaprb has asked for the wisdom of the Perl Monks concerning the following question:

I have a strong feeling this is a solved problem.

I've got a program that turns a list of hashrefs into a tabular display. Right now you can tell it to display columns qw(a b c) and it'll iterate over the list of hashrefs and pull out elements a, b and c, then table-format and print them.

I want to give my users more power. I want them to be able to say the columns are a/b, b+c, c*a/(b+a) and so forth. Sounds a lot like awk, eh? For various reasons I can't just use awk.

The context for each expression to be evaluated will be a single hashref. I don't need to refer to previous hashrefs in the iteration, aggregate over them, or whatnot.

The list of operators will include basic math operators. At this point I can't think why anyone would need more than that. And I don't think I need functions, like sin() or whatnot.

Keys in the hashrefs are word characters, no spaces etc. Actually, each hashref is the union of SHOW STATUS and SHOW VARIABLES from MySQL, plus some other similar stuff thrown in for good measure.

I think I need to parse the user-entered expressions into subs that I can execute repeatedly, because I might be munching through a ton of data. How do I do this? I thought there would be a CPAN module for this, but I can't find one.

Along the way I'll need to add in error-handling, such as making sure no divide-by-zeros happen. Should I just eval{} constantly to handle that? Is that efficient?.

Right now I've experimentally tried asking users to enter the body of a Perl subroutine with each hashref as the sole argument, but that asks them to know Perl, and makes the expressions more verbose. I don't like this myself, much less want to ask it of my users.

Now I'll sit back and wait for the wisdom :-) Thanks in advance!