加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl提供的编译时功能是什么,其他语言没有?

发布时间:2020-12-15 23:24:57 所属栏目:大数据 来源:网络整理
导读: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 c
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拥有Turing完整徽章,但它与其他语言不同,因为“让用户可以在自己的编译阶段访问其完整的编程功能”.那是什么意思? Perl在编译阶段为我提供了哪些编程能力?

解决方法

没有任何其他语言的Perl功能. Lisp可以做任何事情(Lisp就是一个例子,这里.).因此,也许我们可以将问题缩小到Perl的特性,这使得广泛的行为变得容易.

> BEGIN块(END块也是.),它们在编译期间改变了行为.所以我可以编写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可以编程方式编辑将执行的代码. (源过滤器上的CAVEAT!)(粗略地和大致相当于LISP宏)
>与BEGIN块有些相同的是@INC hooks.因为我可以在开头修改@INC以查看更改加载的内容.我可以在@INC数组的前面设置一个子程序来加载我想要加载的任何东西.钩子可以接收加载Frobnify的请求并通过加载Defrobnify.pm来响应它.
>与此类似的是Symbol Manipuation.加载Defrobnify.pm后,我可以这样做:

*Frobnify:: = *Defrobnify::;

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读