【问题标题】:Perl: What exact features does 'use 5.014' enable?Perl:“使用 5.014”启用了哪些确切功能?
【发布时间】:2011-09-18 14:58:37
【问题描述】:

“使用 5.014”究竟启用了什么?

请有人在这里复制和粘贴,因为我无法在任何 perldoc 中找到它。 (也许我是盲人)。在 'perldoc 功能' 中只有一些 5.10 的东西。或者给我一些网址。

谢谢。

编辑:

请先检查一下,你会回复什么。例如:试试这个:

use 5.008;
$s=1;
say "hello";

你会收到关于“say”的错误信息,因为 perl 5.8 不知道“say”

之后,试试这个:

use 5.014;
$s=1;
say "hello";

你会得到错误

Global symbol "$s" requires explicit package name 

所以,“使用 5.014”启用 use strictuse feature 'say'; - 默认情况下

【问题讨论】:

    标签: perl


    【解决方案1】:

    除了what raj correctly said 关于在旧版本的 Perl 中使用use 5.014 会收到的错误消息之外,您还可以通过阅读source code of feature 找到启用的功能列表。相关部分靠近顶部:

    my %feature_bundle = (
        "5.10" => [qw(switch say state)],
        "5.11" => [qw(switch say state unicode_strings)],
        "5.12" => [qw(switch say state unicode_strings)],
        "5.13" => [qw(switch say state unicode_strings)],
        "5.14" => [qw(switch say state unicode_strings)],
    );
    

    严格位部分隐藏在解释器本身的代码中更深一些。如果您查看pp_ctl.c for tag v5.11.0

    /* If a version >= 5.11.0 is requested, strictures are on by default! */
    
    if (PL_compcv && vcmp(sv, sv_2mortal(upg_version(newSVnv(5.011000), FALSE))) >= 0) {
        PL_hints |= (HINT_STRICT_REFS | HINT_STRICT_SUBS | HINT_STRICT_VARS);
    }
    

    【讨论】:

    • 感谢指针。只是在源代码中没有看到“use strict;”。
    • 很好的答案 - 感谢您找到有助于指定每个捆绑包带来什么的来源。一个问题:strict 也是自动启用的(我记得从 5.12 开始)。你知道在哪里/什么时候完成吗? (据我所知,它没有列在feature的源代码中。
    • 太棒了-谢谢。 (你的确实应该是公认的答案。)
    【解决方案2】:

    在较新的 Perls 中(我认为是从 5.10 开始)use 5.x 隐含 use feature ':5.x' 阅读 5.125.14 的 perldelta,我看到 5.12 中添加了一个与 unicode 相关的功能,但它似乎什么都没有new 是在 5.14 中添加的。

    【讨论】:

    • 感谢您将我指向“deltas”,其中:使用版本号大于或等于 5.11.0 的 use VERSION 语法将在词法上启用限制,就像 use strict 一样(除了启用功能。)谢谢!!!
    • 也看到这个答案:stackoverflow.com/questions/6050031/…
    【解决方案3】:

    use x.x.x pragma 确实启用了一些功能,并且很容易对此进行测试:

    #!/usr/bin/env perl
    use warnings;
    use 5.14.0;
    
    say "hello world!"
    

    运行良好;输出“hello world!”。

    #!/usr/bin/env perl
    use warnings;
    # use 5.14.0;
    
    say "hello world!"
    

    燃烧的死亡;输出此错误消息:

    Unquoted string "say" may clash with future reserved word at foo line 5.
    String found where operator expected at foo line 5, near "say "hello world!""
        (Do you need to predeclare say?)
    syntax error at foo line 5, near "say "hello world!""
    Execution of foo aborted due to compilation errors.
    

    不过,我不能 100% 确定自 5.14.0 起启用了哪些功能。我相信你会得到saystateswitchunicode_stringsstrict

    【讨论】:

      猜你喜欢
      • 2017-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-16
      • 2012-11-29
      • 2016-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多