gpt4 book ai didi

perl - Perl 提供了哪些其他语言没有的编译时功能?

转载 作者:行者123 更新时间:2023-12-02 20:56:03 25 4
gpt4 key购买 nike

Perl 被视为通用编程语言吗?

Wikipedia 上阅读相关内容

Perl has a Turing-complete grammar because parsing can be affected by run-time code executed during the compile phase.[41] Therefore, Perl cannot be parsed by a straight Lex/Yacc lexer/parser combination. Instead, the interpreter implements its own lexer, which coordinates with a modified GNU bison parser to resolve ambiguities in the language.

It is often said that "Only perl can parse Perl," meaning that only the Perl interpreter (perl) can parse the Perl language (Perl), but even this is not, in general, true. Because the Perl interpreter can simulate a Turing machine during its compile phase, it would need to decide the Halting Problem in order to complete parsing in every case. It's a long-standing result that the Halting Problem is undecidable, and therefore not even perl can always parse Perl. Perl makes the unusual choice of giving the user access to its full programming power in its own compile phase. The cost in terms of theoretical purity is high, but practical inconvenience seems to be rare.

因此,它说虽然 Perl 拥有图灵完备徽章,但它与其他语言不同,因为“用户可以在自己的编译阶段访问其完整的编程能力”。这意味着什么? Perl 在编译阶段为我提供了哪些其他人没有提供的编程能力?

最佳答案

Perl 的所有功能都出现在任何其他语言中。 Lisp 可以做任何事情(Lisp 是一个例子,这里。)。因此,也许我们可以将问题缩小到 Perl 的哪些特性使得广泛的行为波动变得很容易。

  • BEGIN block (也是 END block )会改变编译期间的行为。因此我可以编写 Perl 代码来更改要加载的模块的位置。

    即使下面的代码也可能有不同的含义。

    use Frobnify;
    Frobnify->new->initialize;

    因为我可以更改 Frobnify 的加载位置:

    BEGIN { 
    if ( [ localtime ]->[6] == 2 ) {
    s|^/var|/var/days/tuesday| foreach @INC;
    }
    }

    所以在星期二,我加载 /var/days/tuesday/perl/lib/Frobnify.pm

  • Source Filters可以以编程方式编辑将要执行的代码。 (注意源过滤器!)(粗略地大致相当于 LISP 宏)

  • BEGIN block 一起的有 @INC hooks 。因为我可以在开始时修改 @INC 来查看加载内容的变化。我可以在 @INC 数组的前面设置一个子例程来加载我想要加载的任何内容。该 Hook 可以接收加载 Frobnify 的请求,并通过加载 Defrobnify.pm 来响应该请求。

  • 与此相伴的是符号操作。加载 Defrobnify.pm 后,我可以执行以下操作:

    *Frobnify:: = \*Defrobnify::;

    现在Frobnify->new创建一个Defrobnify对象!

关于perl - Perl 提供了哪些其他语言没有的编译时功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3575581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com