为什么 Perl 源过滤器不好,什么时候可以使用它们? [英] Why are Perl source filters bad and when is it OK to use them?

查看:31
本文介绍了为什么 Perl 源过滤器不好,什么时候可以使用它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

源过滤器是常识",不应该用于生产代码.

It is "common knowledge" that source filters are bad and should not be used in production code.

当回答一个类似的,但更具体的问题我找不到任何好的参考资料来清楚地解释为什么过滤器不好以及何时可以安全使用.我认为现在是创建一个的时候了.

When answering a a similar, but more specific question I couldn't find any good references that explain clearly why filters are bad and when they can be safely used. I think now is time to create one.

  1. 为什么源过滤器不好?
  2. 什么时候可以使用源过滤器?

推荐答案

只有 perl 可以解析 Perl(参见 这个例子):

Only perl can parse Perl (see this example):

@result = (dothis $foo, $bar);

# Which of the following is it equivalent to?
@result = (dothis($foo), $bar);
@result = dothis($foo, $bar);

这种歧义使得编写始终成功并做正确事情的源过滤器变得非常困难.当出现问题时,调试很尴尬.

This kind of ambiguity makes it very hard to write source filters that always succeed and do the right thing. When things go wrong, debugging is awkward.

在几次崩溃和烧毁之后,我形成了一种从不尝试编写另一个源过滤器的迷信方法.

After crashing and burning a few times, I have developed the superstitious approach of never trying to write another source filter.

不过,我偶尔会使用 Smart::Comments 进行调试.当我这样做时,我在命令行上加载模块:

I do occasionally use Smart::Comments for debugging, though. When I do, I load the module on the command line:

$ perl -MSmart::Comments test.pl

以避免它可能在生产代码中保持启用状态.

so as to avoid any chance that it might remain enabled in production code.

另见: 无法解析 Perl:正式证明

这篇关于为什么 Perl 源过滤器不好,什么时候可以使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆