No such thing as a small change | |
PerlMonks |
Avoiding Greenspun's Rule; scripting within Perlby rlucas (Scribe) |
on Mar 20, 2012 at 18:36 UTC ( [id://960621]=perlquestion: print w/replies, xml ) | Need Help?? |
rlucas has asked for the wisdom of the Perl Monks concerning the following question: Brethren, I have an application for which I find myself following Greenspun's Tenth Rule. I need a way for users to provide expressions that will be evaluated at runtime. The primary reason for this is to be able to interpolate strings based upon conditions (a sort of templating application), but another use is to provide conditional routing logic for what questions to ask next (in a survey application). I've been down this road before; it always starts out like this in the templating part: And, like this in the conditional routing part:
(Now, don't get me wrong. In general, YAGNI and KISS. And if the above works, and often it does, then OK.) However, two things tend to happen if things mature and expectations increase. First is, you discover that you need the IF/THEN logic in the variable interpolation. (You might then decide, OK, I need a template engine here, and start using Template or Mason or whatever. Or, you might decide that you should combine your interpolation system with your routing system, to simplify syntax etc.) Second is, you discover that the IF/THEN logic isn't enough; you need negation, and nesting IFs, and boolean operators, and grouping/precedence. So, now you find yourself basically creating abstract syntax trees with control structures, grouping, operator definition, reserved words, ... holy crap, you've just fallen prey to Greenspun's Rule (or a corollary). And meanwhile, important business requirements are falling by the wayside as you create your own interpreter within Perl. If this were a C program, I'd think "OK, I need to embed Lua." But this is Perl, after all: a scripting language itself, the exemplar of the string eval, the ultimate hackable environment. (And, all of the objects / methods / functions that I'd want to call or use are Perl native.) Fellow monks, what do you suggest when your Perl app requires an embedded scripting language? (E.g., Lua, Safe.pm, ???) Ideally, the solution would be amenable to syntax-checking, and would either be so safe as to be able to accept mostly arbitrary user input to the script, or would be so parseable/structured that it becomes tractable to build an interface or abstraction layer for building expressions.
Back to
Seekers of Perl Wisdom
|
|