Perl 中是否有需要字符串 eval 的用例?
您能否提供任何确实需要使用 eval EXPR
的示例? 我这么问是因为它通常不被鼓励。
Can you provide any examples where using eval EXPR
is really necessary?
I'm asking because its generally discouraged.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
String eval 是执行以下操作的唯一方法:
的任意字符串String eval 不应用于:
eval 还可以用于在也具有 eval 的不同系统或不同语言之间创建临时通信协议(当然前提是一切都是安全的)。 JSON 可以被视为该方法的更安全但有限的子集)
String eval is the only way to:
String eval should not be used to:
eval can also be used to create ad-hoc communication protocols between different systems or different languages that also have eval (provided that everything is secure of course. JSON can be seen as a more secure, but limited subset of this method)
字符串 eval 是从字符串中生成新代码的唯一方法。这很少是必要的,但有一些有效的用例,例如将模板转换为可运行的代码时。除此之外,它的有效用途很少。
eval "require $module_name"
是一个众所周知的习惯用法,但使用例如 Module::Load 比 IMO 更好。String eval is the only way to make new code out of strings. This is rarely necessary, but there are a few valid use cases, for example when converting templates to runnable code. Other than that, there are very few valid uses for it.
eval "require $module_name"
is a well known idiom, but using for example Module::Load is a better idea than that IMO.eval EXPR
可用于元编程,以防万一您无法依赖 Moose 或类似的
动态创建类的框架。查看一下来源
Exception::Class
来看看这个技术的全部恐怖之处。
附录: (@friedo)
eval
通过符号表操作提供的一项功能是使用 SUPER 并使其按预期工作的能力:
虽然这会(希望)在运行时消失,或者为您设置一个
长达数小时的调试会话:
轻松进入我的 前 5 名我讨厌 Perl 的事情!
eval EXPR
can be used for meta-programming in case you cannotrely on Moose or similar
frameworks to create classes on the fly. Have a look into the source of
Exception::Class
to see the full horror of this technique.
Addendum: (@friedo)
One feature that
eval
provides over symbol table manipulationis the ability to use SUPER and have it work as expected:
While this will (hopefully) die during run-time, or set you up for a
hours-long debugging session:
Easily in my Top 5 of things I hate about Perl!
几年前,我编写了一个静态网站生成器,它使用 Perl(<——有趣的是,我的拼写检查器建议用“Peril”来替代它)作为脚本语言。它允许用户输入有关层次结构不同级别的信息 - 第一个市场是墓地,在那里你有拥有纪念碑的客户(付费客户)(显然是为死者)。
它有一个 GUI,您可以在其中输入变量(这里为我的图形技能而颤抖):
以及图片变量。
此外,对于每个市场(例如前面提到的墓地),都有一个控制脚本,该脚本基本上在多个层次结构级别上运行和处理每个记录以生成静态内容。在此之前,程序已将所有这些变量转换为如下内容:(
我认为语法是正确的,我已经有一段时间没有进行任何 Perl 开发了)。最终的结果是一个完整的网站,有很多客户、很多纪念馆、搜索页面和一个不错的小赚钱工具(我被葬礼承办人赶出了市场,他们在内部提供与“免费”服务相同的东西)他们的常规包裹)。
但整个事情基本上是用最小的 Perl 编写的,它只做了两件事:
赋值语句和特定于市场的 Perl 代码的执行都是通过 eval 完成的。
所以它并不像你想象的那么无用。这是一种将脚本引入应用程序的廉价方法。
Quite a few years ago, I wrote a static web site generator which used Perl (<-- interesting that my spell checker suggests "Peril" as a replacement for that) as its scripting language. It allowed the user to enter information about different levels of a hierarchy - the first to market was for a cemetery where you had clients (paying customers) who owned memorials (for the deceased, obviously).
It had a GUI where you entered variables such as (tremble at my graphical skills here):
and picture variables as well.
In addition, for each market (such as the afore-mentioned cemetery), there was a controlling script which basically ran and processed each record at multiple hierarchy levels to generate the static content. Before then, the program had turned all those variables into things like:
(I think that syntax is right, I haven't done any Perl development for a while). The end result was a full web site with lots of clients, lots of memorials, search pages and a nice little money earner while it lasted (I was blown out of the market by funeral directors providing the same thing as a 'free' service within their regular packages).
But the whole thing was basically written in minimal Perl which only did two things:
Both the execution of the assignment statements and the market-specific Perl code was done with eval.
So it's not as useless as you may think. It was a cheap way to introduce scripting into an application.