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!


In reply to Parse user-entered expressions into subs for an awk-like program by xaprb

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.