Perl 中是否有需要字符串 eval 的用例?

发布于 2024-08-22 19:44:30 字数 64 浏览 3 评论 0原文

您能否提供任何确实需要使用 eval EXPR 的示例? 我这么问是因为它通常不被鼓励。

Can you provide any examples where using eval EXPR is really necessary?
I'm asking because its generally discouraged.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

白日梦 2024-08-29 19:44:30

String eval 是执行以下操作的唯一方法:

  1. 在运行时决定的包中执行新代码
  2. 在运行时向包添加重载(至少根据 override.pm)
  3. 执行 Perl
  4. 编译宏类型替换

的任意字符串String eval 不应用于:

  1. 评估符号
  2. 引用执行用户
  3. 陷阱错误
  4. 中的任何代码(如果有其他方法的话)

eval 还可以用于在也具有 eval 的不同系统或不同语言之间创建临时通信协议(当然前提是一切都是安全的)。 JSON 可以被视为该方法的更安全但有限的子集)

String eval is the only way to:

  1. execute new code in a runtime decided package
  2. add overloading to a package at runtime (at least according to overload.pm)
  3. execute arbitrary strings of Perl
  4. compile macro type substitutions

String eval should not be used to:

  1. evaluate symbolic references
  2. execute any code from the user
  3. trap errors
  4. if there is any other way to do it

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)

酒几许 2024-08-29 19:44:30

字符串 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.

素年丶 2024-08-29 19:44:30

eval EXPR 可用于元编程,以防万一您无法
依赖 Moose 或类似的
动态创建类的框架。查看一下来源
Exception::Class
来看看这个技术的全部恐怖之处。

附录: (@friedo)
eval 通过符号表操作提供的一项功能
是使用 SUPER 并使其按预期工作的能力:

package Foo;
sub msg { return 'Foo' }

package MyCodeGenerator;

use strict;
use warnings;

eval <<'EOPERL'; # '' -> no variable interpolation
  package FooBar;
  @FooBar::ISA = qw/Foo/;
  sub msg {
    my $self = shift;
    return $self->SUPER::msg() . 'Bar'; # calls &Foo::msg
  }
EOPERL

虽然这会(希望)在运行时消失,或者为您设置一个
长达数小时的调试会话:

package BROKEN_Generator;

@FooBar::ISA = qw/Foo/;
*{'FooBar::msg'} = sub {
  my $self = shift;
  return $self->SUPER::msg() . 'Bar'; # tries to call &BROKEN_Generator::msg
}

轻松进入我的 前 5 名我讨厌 Perl 的事情

eval EXPR can be used for meta-programming in case you cannot
rely 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 manipulation
is the ability to use SUPER and have it work as expected:

package Foo;
sub msg { return 'Foo' }

package MyCodeGenerator;

use strict;
use warnings;

eval <<'EOPERL'; # '' -> no variable interpolation
  package FooBar;
  @FooBar::ISA = qw/Foo/;
  sub msg {
    my $self = shift;
    return $self->SUPER::msg() . 'Bar'; # calls &Foo::msg
  }
EOPERL

While this will (hopefully) die during run-time, or set you up for a
hours-long debugging session:

package BROKEN_Generator;

@FooBar::ISA = qw/Foo/;
*{'FooBar::msg'} = sub {
  my $self = shift;
  return $self->SUPER::msg() . 'Bar'; # tries to call &BROKEN_Generator::msg
}

Easily in my Top 5 of things I hate about Perl!

倾城月光淡如水﹏ 2024-08-29 19:44:30

几年前,我编写了一个静态网站生成器,它使用 Perl(<——有趣的是,我的拼写检查器建议用“Peril”来替代它)作为脚本语言。它允许用户输入有关层次结构不同级别的信息 - 第一个市场是墓地,在那里你有拥有纪念碑的客户(付费客户)(显然是为死者)。

它有一个 GUI,您可以在其中输入变量(这里为我的图形技能而颤抖):

+------------+----------------------------+
| ClientName | Pax Diablo                 |
+------------+----------------------------+
| Address    | 666 Hades St, Washinton DC |
+------------+----------------------------+

以及图片变量。

此外,对于每个市场(例如前面提到的墓地),都有一个控制脚本,该脚本基本上在多个层次结构级别上运行和处理每个记录以生成静态内容。在此之前,程序已将所有这些变量转换为如下内容:(

$Var{"ClientName"} = "Pax Diablo";

我认为语法是正确的,我已经有一段时间没有进行任何 Perl 开发了)。最终的结果是一个完整的网站,有很多客户、很多纪念馆、搜索页面和一个不错的小赚钱工具(我被葬礼承办人赶出了市场,他们在内部提供与“免费”服务相同的东西)他们的常规包裹)。

但整个事情基本上是用最小的 Perl 编写的,它只做了两件事:

  • 将变量转换为 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):

+------------+----------------------------+
| ClientName | Pax Diablo                 |
+------------+----------------------------+
| Address    | 666 Hades St, Washinton DC |
+------------+----------------------------+

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:

$Var{"ClientName"} = "Pax Diablo";

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

  • turned the variables into Perl assignment statements; and
  • ran the market-specific Perl code.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文