Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2024-03-28 18:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found