I'm writing a database maintenance app whose operation is controlled from a database table. One type of entry in the table allows calling a sub in the program with a specified argument. (The program checks that the table entry is on a list of safe-to-call subs.)
I can think of two ways to dispatch to the code based on what I've read in. One is to use a symbolic reference:
# Suppose $op is the name of the sub,
# and $arg is the argument string to pass to it.
if (is_a_safe_sub($op))
{
no strict 'refs';
$op->($arg) or warn "Error, blah, blah";
}
Another way is to use
eval:
if (is_a_safe_sub($op))
{
eval "$op($arg)" or warn "Error, blah, blah";
}
I made a hit/miss list that looks like this:
- eval EXPR is a lot slower. (Although this will only be called once a day or so.)
- The no strict 'refs' makes my eyes hurt. (I'll get used to it.)
- With eval, the sub can die without taking the app down. (In fact, the code doesn't do this, so the point's moot).
An additional consideration is that the people who'll maintain this aren't very strong in Perl; but I'm not sure which construct would be less confusing.
So, brothers and sisters, I humbly seek your counsel. Which way is better, and why? Is there yet a third way I haven't thought about?
TIA
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.